Binary Tree Preorder Traversal leetcode java
题目:
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]
.
Note: Recursive solution is trivial, could you do it iteratively?
题解:
递归做法如下:
1 public void helper(TreeNode root, ArrayList<Integer> re){
2 if(root==null)
3 return;
4 re.add(root.val);
5 helper(root.left,re);
6 helper(root.right,re);
7 }
8 public ArrayList<Integer> preorderTraversal(TreeNode root) {
9 ArrayList<Integer> re = new ArrayList<Integer>();
if(root==null)
return re;
helper(root,re);
return re;
}
非递归方法:
1 public ArrayList<Integer> preorderTraversal(TreeNode root) {
2 ArrayList<Integer> res = new ArrayList<Integer>();
3 if(root == null)
4 return res;
5 LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
6 while(root!=null || !stack.isEmpty()){
7 if(root!=null){
8 stack.push(root);
9 res.add(root.val);
root = root.left;
}
else{
root = stack.pop();
root = root.right;
}
}
return res;
}
Binary Tree Preorder Traversal leetcode java的更多相关文章
- Binary Tree Postorder Traversal leetcode java
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...
- Binary Tree Preorder Traversal —— LeetCode
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Binary Tree Preorder Traversal -- leetcode
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
- Binary Tree Inorder Traversal leetcode java
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binar ...
- Binary Tree Preorder Traversal -- LEETCODE 144
方法一:(迭代) class Solution { public: vector<int> preorderTraversal(TreeNode* root) { vector<in ...
- Binary Tree Preorder Traversal on LeetCode in Java
二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...
- LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- LeetCode: Binary Tree Preorder Traversal 解题报告
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
随机推荐
- Windows10怎么架设局域网DNS服务器?
已采纳 需要安装Windows组件进行设置.最好是安装服务器版本的Windows. 1. 安装DNS服务 开始—〉设置—〉控制面板—〉添加/删除程序—〉添加/删除Windows组件—〉“网络服务”—〉 ...
- 解决命令行运行python文件,出现No module named *** 报错问题
有时候在一个项目中运行的时候,可能是之前已经mark成sources root 你自己忘记了, 于是就在命令行也执行python文件,然后就出现 No module named *** 等 相关你认为 ...
- Hadoop Hive概念学习系列之hive里的JDBC编程入门(二十二)
Hive与JDBC示例 在使用 JDBC 开发 Hive 程序时, 必须首先开启 Hive 的远程服务接口.在hive安装目录下的bin,使用下面命令进行开启: hive -service hives ...
- CentOS配置远程日志服务器
(1).发送日志的服务器(被收集) [root@xuexi ~]# vim /etc/rsyslog.conf //在#*.* @@remote-host:514行下添加一行 *.* @@192.16 ...
- insert ignore duplicate key
Insert into T1select * from T2 where NOT EXISTS (select 1 from T1 X where X.GUID=T2.GUID);
- date time insert
DATE=`date '+%m/%d/%Y'`TIME=`date '+%H:%M:%S'` sed -i '1i1***** start*****' test.kshsed -i '2i\ REPO ...
- python opencv3 窗口显示摄像头的帧
git:https://github.com/linyi0604/Computer-Vision # coding:utf8 import cv2 """ 在窗口显示摄像 ...
- bzoj 4242 水壶 (多源最短路+最小生成树+启发式合并)
4242: 水壶 Time Limit: 50 Sec Memory Limit: 512 MBSubmit: 1028 Solved: 261[Submit][Status][Discuss] ...
- UVALive 5971
Problem J Permutation Counting Dexter considers a permutation of first N natural numbers good if it ...
- Centos安装Perforce
Author: JinDate: 20140827System: CentOS release 6.5 (Final) 参考:http://www.cnblogs.com/itech/archive/ ...