【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?

解答:

这题难在使用迭代而不是递归。思考一下,先序遍历过程中是不是处理根之后,根的左子树变成新的根,还有子树继续变成根……是不是很像一种数据结构:

思路是这样:

  1. vector 存入当前根(先序处理根);
  2. 处理根之后再去处理左子树,左子树成为下一轮的根。那右子树怎么办?存入栈中;
  3. 左子树都处理完了,出栈最近的右子树成为新根。
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode *root) {
        vector<int> result;
        result.clear();
        if(root == NULL)    return result;

        stack<TreeNode*> right;
        TreeNode* tmp = root;
        bool tag = true;

        while(tag || !right.empty())
        {
            tag = false;
            while(tmp != NULL)
            {
                if(tmp->right != NULL)
                {
                    right.push(tmp->right);
                }

                result.push_back(tmp->val);
                tmp = tmp->left;
            }

            if(!right.empty())
            {
                tag = true;
                tmp = right.top();
                right.pop();
            }
        }

        return result;
    }
};

代码有几个注意点:

  1. 处理空树;
  2. tag 的使用:防止最开始栈为空的情况、防止栈中取最后一个右子树之后但还未处理的情况。

后来找到一份更为简洁的代码,自己果然还是太嫩……

class Solution {
public:
    vector<int> preorderTraversal(TreeNode *root) {
        vector<int> result;
        stack<TreeNode*> s;
        if (root) {
            s.push(root);
        }

        while(!s.empty()) {
            TreeNode* n = s.top();
            s.pop();
            while(n) {
                if (n->right) {
                    s.push(n->right);
                }
                result.push_back(n->val);
                n = n->left;
            }
        }
        return result;
    }
};
时间: 2024-10-16 20:10:23

【LeetCode从零单刷】Binary Tree Preorder Traversal的相关文章

LeetCode之“树”:Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder 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][JavaScript]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? https://leetcod

[LeetCode 题解]: 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? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

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

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 @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 题意:这题用递归比较简单.应该考察的是使用非递归实现二叉树的先序遍历.先序遍历的遍历顺序是:根,左子树,右子树. 解题思路:如果树为下图: 1 /     \ 2         3 /     \    /    \ 4       5  6     7 使用一个栈.步骤为: 一,先遍历节点1,并入栈,如果有左孩子,继续遍历并入栈,一直到栈为{1,2,4}.

[Leetcode][Tree][Binary Tree Preorder Traversal]

二叉树的前序遍历:root点先被访问,然后是left和right子节点.迭代的版本也相对好写. 1.递归版本:时间复杂度O(N),空间复杂度O(N) 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }

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(Tree

Leetcode 树 Binary Tree Preorder Traversal

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Binary Tree Preorder Traversal Total Accepted: 17948 Total Submissions: 51578 Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2

leetcode 刷题之路 79 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? 非递归实现树的前序遍历.前序遍历比较简单,注意入栈的时候应该按照右孩子节点在前的顺序,这样