Binary Tree Preorder Traversal —— LeetCode
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?
题目大意:非递归方式实现二叉树的前序遍历。
解题思路:自己实现一个栈,循环(根节点直接入栈并且入res List,循环入栈左子树,pop栈,入栈右子树)。
public class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode curr = root;
        while(curr!=null||!stack.isEmpty()){
            while(curr!=null){
                stack.push(curr);
                res.add(curr.val);
                curr=curr.left;
            }
            curr = stack.pop();
            curr = curr.right;
        }
        return res;
    }
}
Binary Tree Preorder Traversal —— LeetCode的更多相关文章
- Binary Tree Preorder Traversal leetcode java
		题目: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given bina ... 
- Binary Tree Preorder Traversal  -- leetcode
		Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ... 
- Binary Tree Preorder Traversal -- LEETCODE 144
		方法一:(迭代) class Solution { public: vector<int> preorderTraversal(TreeNode* root) { vector<in ... 
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
		144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ... 
- LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
		144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ... 
- 【LeetCode】Binary Tree Preorder Traversal
		Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ... 
- Binary Tree Preorder Traversal on LeetCode in Java
		二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ... 
- 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)
		Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ... 
- LeetCode: Binary Tree Preorder Traversal  解题报告
		Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ... 
随机推荐
- java InputStream
			java InputStream 当为网络数据流是,不能以read为-1作为数据结束的尾. 而用下列案例获取数据. Log.v(TAG, "==========start========== ... 
- A除以B_2
			本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入格式: 输入在1行中依次给出A和B,中间以1空格分隔. 输出格 ... 
- Nginx配置http强制跳转到https
			目的:访问http://sdk.open.test.com/时强制自动跳转到https://sdk.open.test.com/ 修改nginx站点配置文件sdk.open.test.com.conf ... 
- hdoj 1087 (DP)
			代码: #include<iostream> #include<cmath> using namespace std; int a[1005], dp[1005]; ... 
- Direct 2D实现界面库 (1)
			大学时尝试过很多次写一个UI库, 初次使用 GDI 绘图, 当时水平很低, GDI功能太弱, 以失败而告终. 之后使用 GDI+ 绘图, 当时水平依旧很低, GDI功能很强, 但效率实在太慢, 以失败 ... 
- css3基础教程十三征服CSS3选择器
			:enabled选择器 在Web的表单中,有些表单元素有可用(“:enabled”)和不可用(“:disabled”)状态,比如输入框,密码框,复选框等.在默认情况之下,这些表单元素都处在可用状态.那 ... 
- Cocos2dx开发(2)——Win8.1下Cocod2dx 3.2环境搭建
			正式开始搭建cocos2dx环境,回到熟悉的VS 1.Python安装配置 这一步很简单,下载Python2.7.3,笔者直接用软件助手直接下载安装,最后配置环境变量 如下成功 2.cocos2dx ... 
- PHP 用Class构造JSON数据
			header('Content-type: appliction/json; charset=shift-JIS'); // error //{ // "result": fals ... 
- xml 个人练习2
			package cn.gdpe.xml; import java.io.File;import java.io.FileInputStream;import java.io.IOException;i ... 
- win7访问windows server 2003服务器出现未知的用户名或者错误的密码(转载)
			直接放答案,感谢网友提供答案,否则自已还一直在纳闷,为什么? win7系统的安全机制限制了登陆.只要系统时间和win2003服务器的系统时间相差很多,系统就会阻止其登陆,并显示错误信息:"未 ... 
