鏈表演算法_演算法

來源:互聯網
上載者:User

http://www.jianshu.com/p/0190985635eb

package com.jiangshuai.test;import java.util.ArrayList;import java.util.Stack;public class Tree {private class TreeNode{TreeNode left;TreeNode right;int value;}//求最大深度private int maxDepth(TreeNode node){if(node == null){return 0;}int left = maxDepth(node.left);int right = maxDepth(node.right);return Math.max(left, right)+1;}//求二叉樹最小深度private int getMinDepth(TreeNode root){if(root == null){return 0;}if(root.left == null && root.right == null){return 1;}return Math.min(getMinDepth(root.left), getMinDepth(root.right))+1;}//求二叉樹節點個數private int numOfNode(TreeNode root){if(root == null){return 0;}int left = numOfNode(root.left);int right = numOfNode(root.right);return left+right+1;}//求二叉第k層節點的個數private int numOfK(TreeNode root,int k){if(root == null || k<1){return 0;}if(k == 1){return 1;}int numLeft = numOfK(root.left, k-1);int numRight = numOfK(root.right, k-1);return numLeft + numRight;}//判斷二叉樹是否是平衡二叉樹private boolean isBalanced(TreeNode root){return maxDepthF(root) != -1;}private int maxDepthF(TreeNode root){if(root == null){return 0;}int left = maxDepthF(root.left);int right = maxDepthF(root.right);if(left == -1 || right == -1 || Math.abs(left - right) > 0){return -1;}return Math.max(left, right)+1;}//判斷兩個二叉樹是否完全相同private boolean isSame(TreeNode root1,TreeNode root2){if(root1 == null && root2 == null){return true;}else if(root1 == null || root2 == null){return false;}if(root1.value != root2.value){return false;}return isSame(root1.left,root2.left) && isSame(root2.left,root2.right);}//是否互為鏡像二叉樹private boolean isMirror(TreeNode root1,TreeNode root2){if(root1 == null && root2 ==null){return true;}else if(root1 == null || root2 == null){return false;}return isMirror(root1.left, root2.right) && isMirror(root1.right, root2.left);}//翻轉二叉樹    TreeNode mirrorTreeNode(TreeNode root){        if(root == null){            return null;        }        TreeNode left = mirrorTreeNode(root.left);        TreeNode right = mirrorTreeNode(root.right);        root.left = right;        root.right = left;        return root;    }        //二叉樹的前序走訪    private ArrayList<Integer> preOrder(TreeNode root){    //建立一個棧 stack    Stack<TreeNode> stack = new Stack<TreeNode>();    //建立一個ArrayList 用來輸出    ArrayList<Integer> list = new ArrayList<Integer>();    if(root == null){    return list;    }    stack.push(root);    while(!stack.empty()){    TreeNode node = stack.pop();    //根    list.add(node.value);    if(root.right != null){    //右    stack.push(root.right);    }    if(root.left != null){    //左    stack.push(root.left);    }    }    return list;    }        //前序走訪和後續遍曆構造二叉樹    TreeNode buildTreeNode(int[] preorder,int[] inorder){        if(preorder.length!=inorder.length){            return null;        }        return myBuildTree(inorder,0,inorder.length-1,preorder,0,preorder.length-1);    }    TreeNode myBuildTree(int[] inorder,int instart,int inend,int[] preorder,int prestart,int preend){        if(instart>inend){            return null;        }        TreeNode root = new TreeNode(preorder[prestart]);        int position = findPosition(inorder,instart,inend,preorder[start]);        root.left = myBuildTree(inorder,instart,position-1,preorder,prestart+1,prestart+position-instart);        root.right = myBuildTree(inorder,position+1,inend,preorder,position-inend+preend+1,preend);        return root;    }    int findPosition(int[] arr,int start,int end,int key){        int i;        for(i = start;i<=end;i++){            if(arr[i] == key){                return i;            }        }        return -1;    }}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.