Binary Tree Preorder Traversal
来源:互联网 发布:作业调度算法 编辑:程序博客网 时间:2024/05/16 14:54
https://oj.leetcode.com/problems/binary-tree-preorder-traversal/
题目要求用迭代方法对树进行先根遍历,用一个栈来实现,要先放右子节点,再放左子节点。
public class Solution { public List<Integer> preorderTraversal(TreeNode root) { ArrayList<Integer>list = new ArrayList<Integer>();if(root == null)return list;Stack<TreeNode>stack = new Stack<TreeNode>();stack.add(root);while(!stack.empty()){TreeNode node = stack.pop();list.add(node.val);if(node.right!=null){stack.add(node.right);}if(node.left!=null){stack.add(node.left);}}return list; }}
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
- SCN详解
- 【软工学习】优化管理
- 电脑正常开机进入安全模式,不管怎样设置都无法进入正常模式解决
- Linux的Control-M
- Dreamweaver中设置PHP的文件编码方式
- Binary Tree Preorder Traversal
- VirtualBox下安装Mac OS X系统
- 二维数组名做参数传递问题
- 花店橱窗设计 (动态规划)
- 索引的几种访问方法-续(SKIP SCAN)
- Oracle Flashback技术总结
- 在oracle中where 子句和having子句中的区别
- Where are you from? 你会怎么回答?[摘自沪江英语网]
- Leetcode--Maximum Product Subarray