leetcode做题总结,题目Binary Tree Inorder Traversal 2012/08/27
来源:互联网 发布:淘宝打包计件多少钱 编辑:程序博客网 时间:2024/06/08 07:12
二叉树的中序历遍,非递归算法,使用栈进行深度优先历遍,在每个节点pop出栈的时候访问即可。
public List<Integer> inorderTraversal(TreeNode root) { LinkedList<Integer> l = new LinkedList<Integer>(); Stack<TreeNode> s = new Stack<TreeNode>(); if (root==null)return l; TreeNode n =root; //l.add(n.val); s.push(n); while (true) { while (n.left != null) { n = n.left; s.push(n); //l.add(n.val); } if(s.isEmpty())break; else { n=s.pop(); l.add(n.val); while (n.right==null&&!s.isEmpty()) { n=s.pop(); l.add(n.val); } if(n.right==null&&s.isEmpty())break; n=n.right; //l.add(n.val); s.push(n); } } return l; }
0 0
- leetcode做题总结,题目Binary Tree Inorder Traversal 2012/08/27
- LeetCode题目详解——Binary Tree Inorder Traversal
- 46.leetcode题目:94. Binary Tree Inorder Traversal
- LeetCode: Binary Tree Inorder Traversal
- [LeetCode]Binary Tree Inorder Traversal
- LeetCode Binary Tree Inorder Traversal
- [Leetcode] Binary Tree Inorder Traversal
- Leetcode: Binary Tree Inorder Traversal
- LeetCode Binary Tree Inorder Traversal
- LeetCode : Binary Tree Inorder Traversal
- [Leetcode] Binary Tree Inorder Traversal
- 【leetcode】Binary Tree Inorder Traversal
- [LeetCode]Binary Tree Inorder Traversal
- Leetcode: Binary Tree Inorder Traversal
- [leetcode]Binary Tree Inorder Traversal
- LeetCode-Binary Tree Inorder Traversal
- Leetcode Binary Tree Inorder Traversal
- [leetcode] Binary Tree Inorder Traversal
- kx控制台
- 暴力求解
- 概率密度函数估计简介
- Android知识点
- 成绩为部分客户的个人合计大伟反而刮台风
- leetcode做题总结,题目Binary Tree Inorder Traversal 2012/08/27
- 自旋锁与互斥锁
- 渐变颜色填充
- HDU 3718 KM算法
- 杭电2000;用getchar();吸收掉换行符;
- BUPT Summer Journey #test4 D
- 扩展欧几里得
- 学习日记之模板方法模式和 Effective C++
- 吃奶劲儿就德国和科技耳机规划科热加工和认可