[Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal

【题目】

Given a binary tree, return the preordertraversal of its nodes‘ values.

Example:

Input: [1,null,2,3]
   1
         2
    /
   3

Output: [1,2,3]

【思路】

有参考,好机智,使用堆栈压入右子树,暂时存储。

左子树遍历完成后遍历右子树。

【代码】

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        LinkedList<Integer> ans=new LinkedList<Integer>();
        Stack<TreeNode> tmp=new Stack<TreeNode>();
        while(root!=null){
            ans.add(root.val);
            if(root.right!=null){
                tmp.push(root.right);
            }
            root=root.left;
            if(root==null&&!tmp.isEmpty()){
                root=tmp.pop();
            }
        }
        return ans;
    }
}

原文地址:https://www.cnblogs.com/inku/p/9953915.html

时间: 2024-08-15 00:58:16

[Leetcode 144]二叉树前序遍历Binary Tree Preorder Traversal的相关文章

【LeetCode每天一题】Binary Tree Preorder Traversal(前序遍历)

Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1, null, 1,2,3 ] 1 2 / 3 Output: [1,2,3] Follow up: Recursive solution is trivial, could you do it iteratively? 思路 二叉树的前序遍历方法分为递归法和使用循环辅助栈的方法,递归方法我们在递归左右节点之前先将当

leetcode 589. N叉树的前序遍历(N-ary Tree Preorder Traversal)

目录 题目描述: 解法: 题目描述: 给定一个 N 叉树,返回其节点值的前序遍历. 例如,给定一个 3叉树 : 返回其前序遍历:[1,3,5,6,2,4]. 说明: 递归法很简单,你可以使用迭代法完成此题吗? 解法: /* // Definition for a Node. class Node { public: int val; vector<Node*> children; Node() {} Node(int _val, vector<Node*> _children) {

【Leetcode】【Medium】Binary Tree Preorder Traversal

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]. 解题思路: 二叉树非递归先序遍历,使用栈来保存被遍历到的,但是还没遍历其右子树的结点. 代码: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode

【LeetCode从零单刷】Binary Tree Preorder Traversal

题目: Given a binary tree, return the preorder traversal of its nodes' values. Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 解答: 这题难在使用迭代而不是递归.思考一下,先序遍历过程中是不是处理根之后,根的左子树变成新的根,还有子树

【LeetCode从零单刷】Binary Tree Inorder Traversal

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2].  Note: Recursive solution is trivial, could you do it iteratively? 解答: 其实思路有点类似以前的一篇文章:[LeetCode从零单刷]Binary

LeetCode 144. Binary Tree Preorder Traversal 解题报告

144. Binary Tree Preorder Traversal My Submissions Question Total Accepted: 108336 Total Submissions: 278322 Difficulty: Medium Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3

LeetCode: Binary Tree Preorder Traversal [144]

[题目] 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? [题意] 非递归返回先序遍历结果 [思路] 维护一个栈即可 [代码] /** *

[LeetCode] Binary Tree Preorder Traversal (非递归的先序遍历)

Binary Tree Preorder Traversal 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 解题思路: 二叉树的前序遍历.

leetcode - Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig