40: Binary Tree Preorder Traversal

/************************************************************************/
            /*       40:  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].
             * */
            /****前序遍历 preOrder tree 递归方法实现的*** Time: O(n), Space: O(n).*****************************************************************/
            /*
             * 应用1: 文件目录显示:
             * 显示文件夹时,子文件夹缩进,同级的文件夹缩进相同距离,适合使用前序遍历
             * */

  public List<Integer> preorderTraversal(TreeNode root) {

                List<Integer> source=new ArrayList<Integer>();
                String str="";
                preorderTree(root,source,str);
                return source;
            }

            private void preorderTree(TreeNode node,List<Integer> nodes,String str)
            {
                if(node==null)
                {
                    return;
                }
                str+="--------";
                System.out.println(str+"---->"+node.val);
                nodes.add(node.val);
                preorderTree(node.left,nodes,str);
                preorderTree(node.right,nodes,str);
            }

            /****前序遍历 preOrder tree Time: O(n), Space: O(n) 栈迭代方法实现的********************************************************************/
            /*
             * root-- left --right
             * */
            public List<Integer> preorderTraversal2(TreeNode root)
            {
                List<Integer> results=new ArrayList<Integer>();
                Stack<TreeNode> stack=new Stack<TreeNode>();
                stack.push(root);
                TreeNode node=null;
                while(!stack.isEmpty())
                {
                    node=stack.pop();
                    results.add(node.val);
                    if(node.right!=null)
                    {
                        stack.push(node.right);
                    }
                    if(node.left!=null)
                    {
                        stack.push(node.left);
                    }
                }
                return results;
            }

            /**Threaded tree (Morris)***Time: O(n), Space: O(1)*******************************************************************/
            /*二叉树的Morris遍历可不使用用栈,在常量空间O(1)、线性时间O(n)内实现二叉树的前中后序遍历,且遍历后不破坏二叉树的形状(中间过程允许改变其形状)。
Morris遍历的基本原理是利用所有叶子结点的right指针,指向其后继结点,组成一个环,在第二次遍历到这个结点时,由于其左子树已经遍历完了,则访问该结点。
             * */
时间: 2024-10-29 19:06:21

40: Binary Tree Preorder Traversal的相关文章

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    /   3return [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

[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

题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: 1 struct TreeNode { 2 int val; 3 TreeNode* left; 4 TreeNode* right; 5 TreeNode(int x): val(x), left(NULL),right(NULL) {} 6 }; 7 8 vector<int> preorderTraversal(TreeNode *root) //非递归的前序遍历(用栈实现)

[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 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

Binary Tree Preorder Traversal and 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]. c++版: /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode

[Leetcode][JAVA] Binary Tree Preorder Traversal, Binary Tree Inorder Traversal, 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? 不使用递归前序遍历,可以

Binary Tree Postorder Traversal &amp;&amp; Binary Tree Preorder Traversal

详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal            Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could y