JAvaは1粒の二叉木の根から葉のノードの和の最大値と二叉木の遍歴を求めます


1粒の二叉木の根から葉の節点までの和の最大値を求めます
例えば:1 2 3 4 1 6分岐はそれぞれ:1+2+4=7 1+2+1=4 1+3+6=10なので最大値は10
大体の考え方.
ルートからリーフノードまでのすべてのパスの値を求め、それぞれのパスの和を計算して比較し、最大値を求めます.
  • 第1ステップ:ルートからリーフノードへの経路の集合
  • を求める.
    public static void findPath(Node node, List> paths, List tempPath) {
            //             
            tempPath.add(node);
    
            //           ,     ,                 
            if (node.getLeft() == null && node.getRight() == null) {
                List list = new ArrayList<>();
                list.addAll(tempPath);//             list ,     tempPath   paths ,      tempPath    
                paths.add(list);//           
                tempPath.remove(node);//         ,         ,        
            }
    
            if (node.getLeft() != null) {
                findPath(node.getLeft(), paths, tempPath);
            }
    
            if (node.getRight() != null) {
                if (node.getLeft() != null) {//         ,               ,                ,    
                    int index = tempPath.indexOf(node);//            ,
                    tempPath = tempPath.subList(0, index + 1);//               (       ),        
                }
                findPath(node.getRight(), paths, tempPath);
            }
        }
    
  • 第2ステップ:各パス和を計算し、比較して最大
  • を返す.
    public static int cascSum(Node root){
            List tempPath = new ArrayList();
            List> paths = new ArrayList>();
            findPath(root, paths, tempPath);
    
            int sum = 0;
            for (int i = 0; i < paths.size(); i++){
                List path = paths.get(i);
                int tempSum = 0;
                for(Node node : path){
                    tempSum += node.getValue();
                }
                if (sum < tempSum){
                    sum = tempSum;
                }
            }
            return sum;
        }
    

    二叉木のテストと遍歴
    データの構造とアルゴリズムに対して本当に熟知していないで、自分の短い板で、後続は次第に強化します.次のコードは二叉木の遍歴と上の問題のテストで、すべてあって、ネット上のいくつかの文章とデータ構造の教材を参考にしました
    package com.fat;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Stack;
    
    /**
     * @ClassName Node
     * @Auther LangGuofeng
     * @Date: 2019/9/26/026 21:12
     * @Description:                    
     *   :
     * 1
     * 2 3
     * 4 1 6
     *      :
     * 1+2+4=7
     * 1+2+1=4
     * 1+3+6=10
     *       10
     */
    public class Node {
    
        int value;
        Node left;
        Node right;
    
        public int getValue() {
            return value;
        }
    
        public Node getLeft() {
            return left;
        }
    
        public Node getRight() {
            return right;
        }
    
        public Node(int value) {
            this.value = value;
        }
    
        int calcSum(Node root) {
            int max = 0;
            return 0;
        }
    
        //    
        public static void preOrder(Node root) {
            if (root == null)
                return;
            System.out.println(root.getValue());
            preOrder(root.getLeft());
            preOrder(root.getRight());
        }
    
        //    
        public static void inOrder(Node root){
            if(root == null)
                return;
            inOrder(root.getLeft());
            System.out.println(root.getValue());
            inOrder(root.getRight());
        }
    
        //    
        public static void postOrder(Node root) {
            if (root == null)
                return;
            postOrder(root.getLeft());
            postOrder(root.getRight());
    
            System.out.println(root.getValue());
        }
    
        //   
        //    
        public static void iteratorPre(Node root){
            Stack stack = new Stack();
            stack.push(root);
            //            , , 
            while(!stack.isEmpty()){
                root = stack.pop();
                System.out.println(root.getValue());
                //      ,      ,         
                if(root.getRight() != null)
                    stack.push(root.getRight());
                if(root.getLeft() != null)
                    stack.push(root.getLeft());
            }
        }
    
        //    2
        protected static void iterativePreorder2(Node root) {
            Stack stack = new Stack();
            Node node = root;
            while (node != null || stack.size() > 0) {
                while (node != null) {//        ,      
                    System.out.println(node.value);
                    stack.push(node);
                    node = node.getLeft();
                }
                if (stack.size() > 0) {
                    node = stack.pop();
                    node = node.getRight();
                }
            }
        }
    
        //    
        protected static void iterativeInorder(Node root) {
            Stack stack = new Stack();
            Node node = root;
            while (node != null || stack.size() > 0) {
                //         
                while (node != null) {
                    stack.push(node);
                    node = node.getLeft();
                }
                if (stack.size() > 0) {
                    node = stack.pop();
                    System.out.println(node.value);
                    node = node.getRight();
                }
            }
        }
    
        //    ,  
        protected static void iterativePostorder3(Node root) {
            Stack stack = new Stack();
            Node node = root, prev = root;
            while (node != null || stack.size() > 0) {
                while (node != null) {
                    stack.push(node);
                    node = node.getLeft();
                }
                if (stack.size() > 0) {
                    Node temp = stack.peek().getRight();
                    if (temp == null || temp == prev) {
                        node = stack.pop();
                        System.out.println(node.value);
                        prev = node;
                        node = null;
                    } else {
                        node = temp;
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            Node root = new Node(1);
            Node node1 = new Node(2);
            Node node2 = new Node(3);
            Node node3 = new Node(4);
            Node node4 = new Node(1);
            Node node5 = new Node(6);
            root.left = node1;
            root.right = node2;
            node1.left = node3;
            node1.right = node4;
            node2.right = node5;
    //        Node node6 = new Node(8);
    //        Node node7 = new Node(12);
    //        node5.right = node6;
    //        node5.left = node7;
    
            System.out.println(cascSum(root));
    
    //        findPath(root);
    //        for (List item : mRoutes) {
    //            System.out.println("........." + getNodePath(item));
    //        }
    
    
    //        preOrder(root);
    
    //        Stack n = new Stack();
    //         ArrayList list = findMin(root,n);
    //        //list        
    //        for(int i = 0;i < list.size();i++){
    //            System.out.println(list.get(i));
    //        }
    
    
    //        System.out.println("----------------------------");
    //        preOrder(root);
    //        System.out.println("----------------------------");
    //        inOrder(root);
    //        System.out.println("----------------------------");
    //        postOrder(root);
    //        System.out.println("----------------------------");
    //        iteratorPre(root);
    //        System.out.println("----------------------------");
    //        iterativePreorder2(root);
    //        System.out.println("----------------------------");
    //        iterativeInorder(root);
    //        System.out.println("----------------------------");
    //        iterativePostorder3(root);
        }
    
        public static int cascSum(Node root){
            List tempPath = new ArrayList();
            List> paths = new ArrayList>();
            findPath(root, paths, tempPath);
    
            int sum = 0;
            for (int i = 0; i < paths.size(); i++){
                List path = paths.get(i);
                int tempSum = 0;
                for(Node node : path){
                    tempSum += node.getValue();
                }
                if (sum < tempSum){
                    sum = tempSum;
                }
            }
    
            return sum;
        }
    
        public static void findPath(Node node, List> paths, List tempPath) {
            //             
            tempPath.add(node);
    
            //           ,     ,                 
            if (node.getLeft() == null && node.getRight() == null) {
                List list = new ArrayList<>();
                list.addAll(tempPath);//             list ,     tempPath   paths ,      tempPath    
                paths.add(list);//           
                tempPath.remove(node);//         ,         ,        
            }
    
            if (node.getLeft() != null) {
                findPath(node.getLeft(), paths, tempPath);
            }
    
            if (node.getRight() != null) {
                if (node.getLeft() != null) {//         ,               ,                ,    
                    int index = tempPath.indexOf(node);//            ,
                    tempPath = tempPath.subList(0, index + 1);//               (       ),        
                }
                findPath(node.getRight(), paths, tempPath);
            }
        }
    
    }