687. Longest Univalue Path

Description

Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root.

Note: The length of path between two nodes is represented by the number of edges between them.

Example 1:

Input:

tree

Output:

2

Example 2:

Input:

tree

Output:

2

Note: The given binary tree has not more than 10000 nodes. The height of the tree is not more than 1000.

Solution

DFS

這道題的思路跟"543. Diameter of Binary Tree"完全相同。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {    
    public int longestUnivaluePath(TreeNode root) {
        if (root == null) {
            return 0;
        }
        
        int leftPath = longestUnivaluePath(root.left);
        int rightPath = longestUnivaluePath(root.right);
        int rootPath = univalueDepth(root.left, root) 
            + univalueDepth(root.right, root);
    
        return Math.max(rootPath, Math.max(leftPath, rightPath));
    }
    
    public int univalueDepth(TreeNode node, TreeNode target) {
        if (node == null || node.val != target.val) {
            return 0;
        }
        
        return 1 + Math.max(univalueDepth(node.left, target)
                            , univalueDepth(node.right, target));
    }
}

或者這樣寫(xiě),更清晰,而且是O(n):

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int longestUnivaluePath(TreeNode root) {
        return dfs(root)[1];
    }
    
    private int[] dfs(TreeNode root) {
        if (root == null) {
            return new int[] {0, 0};
        }
        
        int noCrossRoot = 0;
        int crossRoot = 0;
        int subMax = 0;

        if (root.left != null) {
            int[] left = dfs(root.left);
            if (root.val == root.left.val) {
                noCrossRoot = 1 + left[0];
                crossRoot = noCrossRoot;
            }
            subMax = Math.max(left[1], subMax);
        }
        
        if (root.right != null) {
            int[] right = dfs(root.right);
            if (root.val == root.right.val) {
                noCrossRoot = Math.max(1 + right[0], noCrossRoot);
                crossRoot += 1 + right[0];
            }
            subMax = Math.max(right[1], subMax);
        }
        
        return new int[] {noCrossRoot, Math.max(crossRoot, subMax)};
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • html,xml,xhtml有什么區(qū)別? -html是語(yǔ)法較為松散的,不嚴(yán)格的web語(yǔ)言-xml是擴(kuò)展標(biāo)記語(yǔ)言,主...
    取個(gè)名字都不行閱讀 162評(píng)論 0 0
  • 周末去陸總貌似成了慣例。晚上7點(diǎn)和伯伯一起走出醫(yī)院陪他去公交車(chē)站,看著他拎著飯盒奔向613的背影,我想這醫(yī)院短期內(nèi)...
    蘇小文S閱讀 367評(píng)論 0 1
  • 為什么選擇在大年初二看《狼圖騰》?大約是因?yàn)榇竽瓿跻豢戳恕栋拈T(mén)風(fēng)云2》吧。 雖然是一個(gè)電影愛(ài)好者,但是已經(jīng)很久沒(méi)有...
    言?shī)?/span>閱讀 385評(píng)論 0 1