Detailed Explanation of Android Advanced Interview Questions Binary Tree Traversal Steps

Please tell me about the binary tree traversal steps?

What is this question trying to investigate?

1. The basic principle and traversal method of binary tree?

Knowledge points of inspection

The basic flow of binary tree traversal, the basic principle of binary tree

How Candidates Answer
Basic concept of binary tree

Simply understood, a tree that satisfies the following two conditions is a binary tree:

  1. itself is an ordered tree;
  2. The degree of each node contained in the tree cannot exceed 2, that is, it can only be 0, 1 or 2;
    3.insert image description here

Properties of Binary Trees

A binary tree has the following properties:

  1. In a binary tree, the i-th layer has at most 2i-1 nodes.
  2. If the depth of the binary tree is K, then the binary tree has at most 2K-1 nodes.
  3. In a binary tree, the number of terminal nodes (number of leaf nodes) is n0, and the number of nodes with degree 2 is n2, then n0=n2+1.
Binary tree traversal

insert image description here

Binary tree traversal methods mainly include: pre-order traversal, in-order traversal, post-order traversal, and hierarchical traversal. Preorder, inorder, and postorder actually refer to the order in which the parent nodes are visited. If during the traversal process, the parent node is visited before its child nodes, it is a pre-order traversal; the order in which the parent node is visited is between the left and right child nodes, which is an in-order traversal; after visiting the left and right child nodes, visit the parent node , which is post-order traversal. Whether it is pre-order traversal, middle-order traversal or post-order traversal, the relative access order of the left and right child nodes remains unchanged, always visit the left child node first, and then visit the right child node. Hierarchical traversal is to visit each node of the binary tree in order from top to bottom and from left to right.

preorder traversal

code show as below:

//filename: BinTreeNode.h
template <typename T>
void travPre_R(BinTreeNode<T> * root) {
    
    //二叉树先序遍历算法(递归版)
    if (!root) return;
    cout << root->data;
    travPre_R(root->LeftChild);
    travPre_R(root->RightChild);
}

Inorder traversal

code show as below:

template <typename T>
void travIn_R(BinTreeNode<T> * root) {
    
    //二叉树先序遍历算法(递归版)
    if (!root)
        return;
    travPre_R(root->LeftChild);
    cout << root->data;
    travPre_R(root->RightChild);
}

1.8 Use recursive and non-recursive to traverse the binary tree?

What is this question trying to investigate?

1. The basic principle and traversal method of binary tree?

Knowledge points of inspection

The basic concept of binary tree traversal, the basic principle of binary tree

How Candidates Answer
Basic concept of binary tree

Simply understood, a tree that satisfies the following two conditions is a binary tree:

  1. itself is an ordered tree;
  2. The degree of each node contained in the tree cannot exceed 2, that is, it can only be 0, 1 or 2;

insert image description here

Binary tree traversal
  • Preorder traversal: The traversal order of each tree is: root node→left node→right node. The preorder traversal output of the above figure is: FCADBEHGM
  • In-order traversal: The traversal order of each tree is: left node→root node→right node. The preorder traversal output of the above figure is: ACBDFHEMG
  • Post-order traversal: The traversal order of each tree is: left node→right node→root node. The preorder traversal output of the above figure is: ABDCHMGEF

preorder traversal

递归法:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    
    
    private List<Integer> res = new ArrayList<>();
    public List<Integer> preorderTraversal(TreeNode root) {
    
    
        //中 ——> 左 ——> 右
        preorder(root);
        return res;
    }
    
    private void preorder(TreeNode node){
    
    
        if(node == null) return;
        res.add(node.val);
        preorder(node.left);
        preorder(node.right);
    }
}

非递归法:
===基本的算法思想===
创建一个栈,用来储存遍历的轨迹:
1.如果栈不为空则储存当前栈顶元素的值,并弹栈;
2.如果栈顶元素存在右儿子,将右儿子压入;
3.如果栈顶元素有左儿子,将左儿子压入
4.重复1直至栈为空
    
/**
 * 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) {
    
    
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()){
    
    
            if(cur != null){
    
    
                res.add(cur.val);
                stack.push(cur);
                cur = cur.left;
            }else{
    
    
                cur = stack.pop();
                cur = cur.right;
            }
        }
        return res;
    }
} 

Inorder traversal

递归法:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    private List<Integer> res = new ArrayList<>();
    public List<Integer> inorderTraversal(TreeNode root) {
        inorder(root);
        return res;
    }
    
    public void inorder(TreeNode node){
        if(node == null) return;
        inorder(node.left);
        res.add(node.val);
        inorder(node.right);
    }
}

非递归法:
class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()){
            //压栈
            if(cur != null){
                stack.push(cur);
                cur = cur.left;
            }else{
                //左边已经存完,弹栈
                cur = stack.pop();
                res.add(cur.val);
                cur = cur.right;
            }
        }
        return res;
    }
}

post order traversal

递归法:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    private List<Integer> res = new ArrayList<>();
    public List<Integer> postorderTraversal(TreeNode root) {
        //左右中
        postorder(root);
        return res;
    }
    
    private void postorder(TreeNode node){
        if(node == null) return;
        postorder(node.left);
        postorder(node.right);
        res.add(node.val);
    }
}

非递归法:
基本的算法思想:
使用栈来记录遍历轨迹,并使用一个变量来储存上一次方法的元素,当当前元素左右儿子为空或当前元素已经在上一轮访问过(即上一次方法访问的元素为当前访问元素的节点),则栈顶元素出栈。
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if(root == null) return res;
        
        Stack<TreeNode> stack = new Stack<>();
        TreeNode pre = null;
        stack.push(root);
        
        while(!stack.isEmpty()){
            TreeNode cur = stack.peek();
            if((cur.left == null && cur.right == null)
              || (pre != null && (cur.right == pre || cur.left == pre))){
                res.add(cur.val);
                pre = cur;
                stack.pop();
            }else{
                if(cur.right != null) stack.add(cur.right);
                if(cur.left != null) stack.add(cur.left);
            }
        }
        
        return res;
    }
}

End of article

More Android interview questions can be scanned for free!

↓↓↓【Preview】↓↓↓

img

Guess you like

Origin blog.csdn.net/Android_XG/article/details/130935063