144. Binary Tree Preorder Traversal (non-recursive preorder traversal of a binary tree)

Given a binary tree, return the preorder traversal of its nodes' values.

Example:

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

Output: [1,2,3]

Follow up: Recursive solution is trivial, could you do it iteratively?

Method One: recursive

The idea is very simple, root around

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public void preorderTraversal(TreeNode root) {
        if(root==null) return ;
        System.out.print(root.val+' ');
        preorderTraversal(root.left);
        preorderTraversal(root.right);
    }
}

Method Two: Iteration

Iterative Solution can all use recursion problem with recursion is nothing more than the use of the system function stack, if they apply for the data structure to replace the function stack, can achieve the same functionality.

Preorder traversal around roots, root added to the stack and then pop, pop up each time, the right and left pop-up node subtree is added to ensure that the order of about pop roots.

/**
 * 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) {
        if(root==null) return null;
        if(root!=null) {
            Stack<TreeNode> stack=new Stack<TreeNode>();
            List<Integer> list=new ArrayList<Integer>();
            stack.add(root);
            while (!stack.isEmpty()){
                root=stack.pop();
                list.add(root.val);
                if(root.right!=null) {
                    stack.add(root.right);
                }
                if(root.left!=null){
                    stack.add(root.left);
                }
            }
        }
        return list;
    }
}

 

Intelligent Recommendation

[Leetcode] 144. Preorder traversal of binary tree (non-recursive, Morris algorithm)

Topic link Click to open title link Title description answer Recursion The recursive solution is very simple, and the code is relatively streamlined. The time complexity is O ( n ) O(n) O(n), The spac...

144. Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. Example: Follow up: Recursive solution is trivial, could you do it iteratively? The binary tree preorders the tr...

144 Binary Tree Preorder Traversal

1 topic 2 Try to solve 3 standard solution  ...

White self spring / start from a basic helloworld

Experiment 1: create objects through the IOC container, and for the property assignment ★ ==================================== Write a helloworld Before their own new objects, now all the objects to c...

More Recommendation

Java - SpringMvc-Spring integration projects

One, SpringMVC and Spring Integration (Spring and mybatis consolidation above has been introduced) 1, the integration of ideas Integration process, accumulation is: Code + + dependent configuration. T...

C / C ++: Some operations of strings

Introduction A string is a very important data type, but the C language does not have an explicit string type, and the string in the C language appears in the form of string constants or in the charac...

C + CUDA acquisition device information

Get device information Article catalog Get device information 1 CUDA introduction 2 C language CUDA environment May need to install VS first 2.1 Install NVCC 2.2 Installing Cudnn 2.3 Run the CUDA prog...

[sword refers to offer] delete duplicate nodes in the linked list

Description of the topic: In a sorted linked list, there are duplicate nodes. Please delete the duplicate nodes in the linked list. The duplicate nodes are not retained, and the linked list header poi...

SpringBoot multi-module construction

SpringBoot multi-module construction Illustration of SpringBoot project creation Submodule creation diagram demo parent project dao submodule biz submodule web submodule run Illustration of SpringBoot...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top