두 갈래 나무의 앞 순서와 중간 순서 (귀속과 비귀속)

4031 단어 정렬

앞의 순서가 두루 미치다.


귀속판
/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */

public class Solution {
    /**
     * @param root: A Tree
     * @return: Preorder in ArrayList which contains node values.
     */
    public List preorderTraversal(TreeNode root) {
        List list = new ArrayList<>();
        preorderTraversal(root, list);
        return list;
    }
    public void preorderTraversal(TreeNode root, List list){
        if(root == null) return;
        list.add(root.val);
        preorderTraversal(root.left, list);
        preorderTraversal(root.right, list);
    }
}

비귀속판
/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */

public class Solution {
    /**
     * @param root: A Tree
     * @return: Preorder in ArrayList which contains node values.
     */
    public List preorderTraversal(TreeNode root) {
        List list = new ArrayList<>();
        Stack stack = new Stack<>();
        if(root == null) return list;
        
        stack.push(root);
        
        while(!stack.empty()){
            TreeNode node = stack.pop();
            list.add(node.val);
            
            // left
            if(node.right != null){
                stack.push(node.right);
            }
            // right
            if(node.left != null){
                stack.push(node.left);
            }
        }
        return list;
    }
}

 

중순으로 두루 다니다.


귀속판
/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */

public class Solution {
    /**
     * @param root: A Tree
     * @return: Inorder in ArrayList which contains node values.
     */
     List list = new ArrayList<>();
    public List inorderTraversal(TreeNode root) {
        if(root == null) return list;
        inorderTraversal(root.left);
        list.add(root.val);
        inorderTraversal(root.right);
        return list;
    }
}

 
비귀속판
/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */

public class Solution {
    /**
     * @param root: A Tree
     * @return: Inorder in ArrayList which contains node values.
     */
    //  
    public List inorderTraversal(TreeNode root) {
        Stack stack = new Stack<>();
        List list = new ArrayList<>();
        
        if(root == null) return list;
        
        while(root != null){
            stack.push(root);
            root = root.left;
        }
        
        while(!stack.empty()){
            TreeNode node = stack.peek();
            list.add(node.val);
            
            if(node.right == null){
                node = stack.pop();
                while(!stack.empty() && stack.peek().right == node){
                    node = stack.pop();
                }
            }
            else{
                node = node.right;
                while(node != null){
                    stack.push(node);
                    node = node.left;
                }
            }
        }
        return list;
    }
}

좋은 웹페이지 즐겨찾기