144. Binary Tree Preorder Traversal
来源:互联网 发布:java社区 编辑:程序博客网 时间:2024/06/15 23:26
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [1,2,3]
.
public class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> list=new ArrayList<>(); Stack<TreeNode> stack=new Stack<>(); TreeNode p=root; while(p!=null||!stack.isEmpty()){ while(p!=null){ list.add(p.val); stack.push(p); p=p.left; } if(!stack.isEmpty()){ p=stack.pop(); p=p.right; } } return list; } }
0 0
- 144.Binary Tree Preorder Traversal
- 144.Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144.Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- 144. Binary Tree Preorder Traversal
- Zurmo(十)之zurmoc所有命令详解(三)
- <<effective c++>>笔记
- c/c++ 头文件梳理
- Python函数式编程
- 回顾2016,畅想2017(20170107)
- 144. Binary Tree Preorder Traversal
- 云计算与分布式的一些关键词
- ViewPager踩坑记
- Tkinter-ListBox基本使用
- PHP中MongoDB数据库的连接、添加、修改、查询、删除等操作实例
- find和grep结合更加方便的查找
- 两个元素的快排 NDU.i
- J - Dungeon Master
- 【考研】东北大学二叉树相关算法(2)