[NowCoder] 树上最长单色路径

http://www.nowcoder.com/question/next?pid=1597148&qid=44665&tid=3119680

对于一棵由黑白点组成的二叉树,我们需要找到其中最长的单色简单路径,其中简单路径的定义是从树上的某点开始沿树边走不重复的点到树上的另一点结束而形成的路径,而路径的长度就是经过的点的数量(包括起点和终点)。而这里我们所说的单色路径自然就是只经过一种颜色的点的路径。你需要找到这棵树上最长的单色路径。
给定一棵二叉树的根节点(树的点数小于等于300,请做到O(n)的复杂度),请返回最长单色路径的长度。这里的节点颜色由点上的权值表示,权值为1的是黑点,为0的是白点。

测试用例:
[0,0,1,1,0,0,1,1,1,0,0,1]
对应输出应该为:4
你的输出为:5

解答;

  1. 记忆化搜索;
    2)注意符号的优先级。。。逗号运算符。。。
import java.util.*;

/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}*/
public class LongestPath {
/*
    记忆化搜索;
    每个节点植被搜索一次
    */
Map<TreeNode,Integer> blkMap = new HashMap<>();
    Map<TreeNode,Integer> whtMap = new HashMap<>();
    Integer maxPath = 0;

    public int findPath(TreeNode root) {
        // write code here
        if(root == null) return 0;
        dfsNode(root);
        return maxPath;
    }

    private void dfsNode(TreeNode root){
        blkPath(root);
        whtPath(root);
        if(root.left != null) dfsNode(root.left);
        if(root.right != null) dfsNode(root.right);
    }

    private int blkPath(TreeNode root){
        if(root.val == 0){
            blkMap.put(root,0);
            return 0;
        }
        int left = 0, right = 0;
        if(root.left != null){
            if(blkMap.get(root.left) == null)
                blkPath(root.left);
            left = blkMap.get(root.left);
        }
        if(root.right!= null){
            if(blkMap.get(root.right) == null)
                blkPath(root.right);
            right = blkMap.get(root.right);
        }
        /*
        ldft -> root -> right
        */
        int path = 1 + left + right;
        maxPath = maxPath >= path ? maxPath:path;
        /*
        parent -> root -> [left|right]
        */
        int maxChild = 1 + (left >= right ? left:right);
        blkMap.put(root,maxChild);
        return maxChild;
    }

    private int whtPath(TreeNode root){
        if(root.val == 1){
            whtMap.put(root,0);
            return 0;
        }

        int left = 0, right = 0;
        if(root.left != null){
            if(whtMap.get(root.left) == null)
                whtPath(root.left);
            left = whtMap.get(root.left);
        }
        if(root.right!= null){
            if(whtMap.get(root.right) == null)
                whtPath(root.right);
            right = whtMap.get(root.right);
        }
        /*
        ldft -> root -> right
        */
        int path = 1 + left + right;
        maxPath = maxPath >= path ? maxPath:path;
        /*
        parent -> root -> [left|right]
        */
        int maxChild = 1 + (left >= right ? left:right);
        whtMap.put(root,maxChild);
        return maxChild;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 树的概述 树是一种非常常用的数据结构,树与前面介绍的线性表,栈,队列等线性结构不同,树是一种非线性结构 1.树的定...
    Jack921阅读 9,932评论 1 31
  • 定义指针变量,如果不赋给它地址,系统会随机给它分配一个地址。 C++标准库 C++ Standard Librar...
    纵我不往矣阅读 2,350评论 0 1
  • 1 序 2016年6月25日夜,帝都,天下着大雨,拖着行李箱和同学在校门口照了最后一张合照,搬离寝室打车去了提前租...
    RichardJieChen阅读 10,610评论 0 12
  • 1 数据2 算法3 线性表4 栈5 队列6 串朴素模式匹配算法 -子串的定位操作:从主串中找到子串KMP模式匹配算...
    oldSix_Zhu阅读 5,401评论 0 4
  • 上帝呀,亲爱的阿爸父,作为一个母亲,我尽力尽我应尽的责任,但是,我有时感觉一个人实在是忙不过来呀,有些事能不让老公...
    从心开始噢阅读 1,397评论 0 0