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>();
10         if(root==null)
11             return re;
12         helper(root,re);
13         return re;
14     }

非递归方法:

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);
10             root = root.left;
11         }
12         else{
13             root = stack.pop();
14             root = root.right;
15         }
16     }
17     return res;
18 }

Binary Tree Preorder Traversal leetcode java

时间: 2024-10-16 19:04:56

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 binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 题解: 递归方法代码: 1     public void helper(Tre

Binary Tree Inorder Traversal leetcode java

题目: 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? 题解: 中序遍历:递归左 处理当前 递归右. 画图的话就是,之前离散老师教的,从ro

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? 算法一,栈 前序遍历. 1.訪问根结点 2.訪问左子树 3.訪问右子树 题目要求不使用递归

Binary Tree Preorder Traversal (leetcode 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? Show Tags Show Similar Problems 可以递归,可以用栈 /

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? 题目大意:非递归方式实现二叉树的前序遍历. 解题思路:自己实现一个栈,循环(根节点直接入栈并

Binary Tree Preorder Traversal -- LeetCode 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]. Solution 1: (递归) /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * Tre

[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? 不使用递归前序遍历,可以

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