LeetCode之maxDepth非递归解法
2014-03-28 20:56
148 查看
int maxDepth_2(TreeNode* root) //非递归求二叉树最大深度 { TreeNode *p,*have_visited; int treeDepth=0; vector<TreeNode*> v_tree; p=root; have_visited=NULL; while(p||!v_tree.empty()) { if (p) { v_tree.push_back(p); p=p->left; } else { p=v_tree.back(); if (p->right==NULL||p->right==have_visited) { treeDepth=treeDepth<v_tree.size()?v_tree.size():treeDepth; have_visited=p; v_tree.pop_back(); p=NULL; } else p=p->right; } } return treeDepth; }
相关文章推荐
- Leetcode Symmetric Tree 递归和非递归解法
- 【LeetCode】109. Convert Sorted List to Binary Search Tree 解法及注释,分治,递归
- leetcode 55. Jump Game(C语言,跳数,递归思想,两种解法)20
- 【LeetCode】104. Maximum Depth of Binary Tree 解法及注释,递归,深度搜索
- [LeetCode系列] 二叉树最大深度求解问题(C++递归解法)
- LeetCode Everyday-- 94,144,145递归解法
- LeetCode 22:Generate Parentheses的递归,回溯两种解法
- 【LeetCode】108. Convert Sorted Array to Binary Search Tree 解法及注释,分治法,递归
- [leetCode]Binary Tree Inorder Traversal 递归 && 栈解法
- Sort List[leetcode] 由归并排序的递归和循环,到本题的两种解法
- Leetcode【二叉树】:Binary Tree Postorder Traversal 题解(非递归解法),可以说是【二叉树】入门必须掌握
- Reverse Nodes in k-Group[leetcode]递归和非递归的解法
- [LeetCode系列]有序链表转换为平衡BST的递归解法
- LeetCode 101. Symmetric Tree 递归解法
- LeetCode path Sum i ,ii递归和迭代解法
- [LeetCode]Reverse Linked List(该题也可以使用递归解法==还没有用递归写过)
- 【LeetCode】99. Recover Binary Search Tree 分析、解法、注释、中序遍历、递归
- [LeetCode系列]N皇后问题递归解法 -- 位操作方式
- LeetCode Binary Tree Preorder Traversal 前序遍历二叉树 递归和非递归解法
- LeetCode Binary Tree Postorder Traversal 二叉树后序遍历 递归和非递归解法