Binary Tree Preorder Traversal
来源:互联网 发布:2009年好听的网络歌曲 编辑:程序博客网 时间:2024/06/09 13:56
地址:https://oj.leetcode.com/problems/binary-tree-preorder-traversal/
题意实现二叉树的前序遍历。利用栈实现。
public class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer > ans = new ArrayList<>(); TreeNode p = root; Stack<TreeNode> st = new Stack<TreeNode>(); while(p!=null || st.size()!=0){ while(p!=null){ ans.add(p.val); st.push(p); p= p.left; } if(st.size()!=0){ p = st.peek(); st.pop(); p = p.right; } } return ans; }}
0 0
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- Binary Tree Preorder Traversal
- android layout将Imageview放在最上层
- Zoj1037
- Python:动态获取对象的属性和方法
- 启程日志
- 01背包 完全背包问题
- Binary Tree Preorder Traversal
- SPI的参考命令集
- PCB走线阻抗的计算
- Binary Tree Zigzag Level Order Traversal
- java中抽象类跟接口的区别
- dedecms
- android 拖拉控件
- 线性时间找第K小的数
- 大数的加法 进位