LeetCode题解:Binary Tree Preorder Traversal
2016-03-01 16:49
302 查看
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].
题意:先序遍历
思路:
代码:
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,2,3].
题意:先序遍历
思路:
代码:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<Integer> preorderTraversal(TreeNode root) { Stack<TreeNode> rights = new Stack<TreeNode>(); List<Integer> result = new ArrayList<Integer>(); while(root != null){ result.add(root.val); if(root.right != null){ rights.push(root.right); } root = root.left; if(root == null && !rights.isEmpty()){ root = rights.pop(); } } return result; } }
相关文章推荐
- java redis使用参考文档
- 我的软件工程目标
- 我的软件工程课目标
- java实现获取字符串strSub在父字符串strFather中出现的次数
- linux下vim学习笔记
- 基于VS平台的多维数据集的建立
- java运行jar文件中某个类
- 一些关于Console的API函数
- 浅议Github的注册和使用
- 作业
- hdoj The first place of 2^n 3215 (数学技巧&预处理)
- spark streaming kafka1.4.1中的低阶api createDirectStream使用总结
- Maven仓库—Nexus环境搭建及简单介绍
- LeetCode题解:Missing Number
- 【找规律】HDOJ Rikka with Chess 5630
- Spring AOP 动态代理
- Android蓝牙开发介绍
- Android必知必会-Stetho调试工具
- VPI 8.6 安装时遇到的问题及解决
- 51nod1354选数字