leetcode 劍指offer刷題歸類之 三 樹

 

 

1.用遞歸和非遞歸兩種方式遍歷二叉樹

public static void inOrderRecur(Node head){
        if(head == null){
            return;
        }
        if(head.left != null){
            inOrderRecur(head.left);
        }
        System.out.print(head.value);
        if(head.right!= null){
            inOrderRecur(head.right);
        }

    }
    public static void preOrderRecur(Node head){
        if(head == null){
            return;
        }
        System.out.print(head.value);
        if(head.left != null){
            preOrderRecur(head.left);
        }

        if(head.right!= null){
            preOrderRecur(head.right);
        }

    }
    public static void posOrderRecur(Node head){
        if(head == null){
            return;
        }

        if(head.left != null){
            posOrderRecur(head.left);
        }

        if(head.right!= null){
            posOrderRecur(head.right);
        }
        System.out.print(head.value);
    }

非遞歸html

/*
      前序遍歷
     */

    public static void preOrderUnRecur(Node head){

        if(head!=null){
            Stack<Node>stack = new Stack<Node>();
            stack.add(head);
            while (!stack.isEmpty()){

                head = stack.pop();
                System.out.print(head.value+" ");
                if(head.right!=null){
                    stack.add(head.right);
                }
                if(head.left!=null){
                    stack.add(head.left);
                }

            }

        }
        System.out.println();
    }


    /*
    中序遍歷
     */
    public static void inOrderUnRecur(Node head){
        if(head!=null){
            Stack<Node> stack = new Stack<Node>();
            while (!stack.isEmpty() || head!=null){
                if(head!=null){
                    stack.push(head);
                    head = head.left;

                }else {
                    head = stack.pop();
                    System.out.println(head.value+" ");
                    head = head.right;
                }
            }

        }
        System.out.println();


    }

    /*
    後續遍歷   左右中是左中右的逆序
     */

    public static void posOrderUnRecur(Node head){

        if(head!=null){

            Stack<Node> stack1 = new Stack<Node>();
            Stack<Node> stack2 = new Stack<Node>();
            stack1.push(head);
            while (!stack1.isEmpty()){
                head = stack1.pop();
                stack2.push(head);
                if(head.left!= null){
                    stack1.push(head.left);
                }
                if(head.right!=null){
                    stack1.push(head.right);
                }

            }
            while (!stack2.isEmpty()){
                System.out.print(stack2.pop().value+" ");
            }

        }
        System.out.println();



    }

2.序列化和反序列化二叉樹

//序列化二叉樹
    public static String xuLieHuaErChaShu(Node head){

        if(head == null){
            return "#!";
        }
        String res = head.value+"!";
        res+=xuLieHuaErChaShu(head.left);
        res+=xuLieHuaErChaShu(head.right);
        return res;
    }

    //反序列化二叉樹
    public static Node reconByPreString(String str){
        String[] values = str.split("!");
        Queue<String> queue = new LinkedList<>();
        for (int i = 0; i < values.length ; i++) {
            queue.add(values[i]);
        }
        return preOrderTree(queue);

    }

    public static Node preOrderTree(Queue<String>queue){
        String value = queue.poll();
        if(value.equals("#")){
            return null;

        }
        Node head = new Node(Integer.valueOf(value));
        head.left = preOrderTree(queue);
        head.right = preOrderTree(queue);
        return head;


    }

3.重建二叉樹

public class ReConstructBinaryTree {
    /**
     *輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。
     * 假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。
     * 例如輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6},
     * 則重建二叉樹並返回
     */
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        return reConBtree(pre,0,pre.length-1,in,0,in.length-1);

    }

    private TreeNode reConBtree(int[] pre, int preStart, int preEnd, int[] in, int inStart, int inEnd) {

        if(preStart>preEnd ||inStart>inEnd){
            return null;
        }
        //根節點
        TreeNode root = new TreeNode(pre[preStart]);

        for (int i = inStart; i <= inEnd ; i++) {
            if(pre[preStart] == in[i]){
                //重構左子樹
                root.left = reConBtree(pre,preStart+1,preStart+i-inStart,in,inStart,i-1);
                //重構右子樹
                root.right = reConBtree(pre,preStart+i+1-inStart,preEnd,in,i+1,inEnd);
                break;

            }

        }
        return root;
    }
}

 

二叉樹的下一個節點java

public Node getNextNode(Node node){
        if(node == null){
            return node;
        }
        
       // 1. node的右子樹不爲null,則找右子樹的最左節點
         
        if(node.right != null){
            return getLeftMost(node.right);
        }else {
            
            /*2.若是node沒有右子樹,那麼先看node是否是node父節點的
            左孩子,若是是,父節點就是他的後繼節點,若是是右孩子,
            就向上尋找node的後繼節點,假設向上移動到的節點記爲s,
            s的父節點記爲p,若是發現s是p的左孩子,那麼節點p就是node節點的後繼節點,
            不然就一直向上移動*/
             
            Node parent = node.parent;
            while (parent != null && parent.left != node){
                node = parent;
                parent = node.parent;
            }
            return parent;
        }
    }

    public Node getLeftMost(Node node){
        if(node == null){
            return node;
        }
        while (node.left != null){
            node = node.left;
        }
        return node;
    }

驗證二叉搜索樹node

---數組

leetcode 108. 將有序數組轉換爲二叉搜索樹ide

public TreeNode sortedArrayToBST(int[] nums) {
        if(nums == null || nums.length == 0){
            return null;
        }

        //包括左邊界,不包括右邊界
        return help(nums,0,nums.length);

    }


    private TreeNode help(int[] nums,int start,int end){
        if(start == end){
            return null;
        }
        int mid = start + (end -start)/2;
        TreeNode node = new TreeNode(nums[mid]);
        node.left = help(nums,start,mid);
        node.right = help(nums,mid+1,end);

        return node;


    }

 

--code

leetcode 113. 路徑總和 IIhtm

public class PathSum113 {
    List<List<Integer>> lists = new ArrayList<>();
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        if(root == null){
            return lists;
        }
        process(root,sum,new ArrayList<>());
        return lists;

    }
    private void process(TreeNode root, int sum, List<Integer>list){
        if(root == null){
            return;
        }
        sum -= root.val;
        list.add(root.val);
        if(root.left == null && root.right == null && 0 == sum){
            lists.add(new ArrayList<>(list));
        }
        process(root.left,sum ,list);
        process(root.right,sum ,list);
        //java中的list傳遞的是引用,因此遞歸結束後,要把以前加入的元素刪除,
        list.remove(list.size()-1);

    }
}
相關文章
相關標籤/搜索