代碼隨想錄算法訓(xùn)練營(yíng)第十八天|513.找樹(shù)左下角的值、112. 路徑總和、113.路徑總和ii、106.從中序與后序遍歷序列構(gòu)造二叉樹(shù)、105.從前序與中序遍歷序列構(gòu)造二叉樹(shù)

513.找樹(shù)左下角的值

513. 找樹(shù)左下角的值 - 力扣(LeetCode)
使用前序遍歷,存儲(chǔ)葉子節(jié)點(diǎn)的值,當(dāng)新的葉子節(jié)點(diǎn)深度大于原來(lái)葉子節(jié)點(diǎn)深度時(shí),再更新value,因?yàn)槭窍缺闅v左節(jié)點(diǎn),又因?yàn)閐eep>Deep時(shí)再更新,所以同一層不會(huì)更新,一直存儲(chǔ)的都是深度最大且在左邊的元素

class Solution {
    //最大節(jié)點(diǎn)深度
    private int Deep = 0;
    //節(jié)點(diǎn)最左側(cè)值
    private int value;
    public int findBottomLeftValue(TreeNode root) {
        findLeftValue(root, 1);
        return value;
    }

    private void findLeftValue(TreeNode node, int deep) {
        if (node == null) {
            return;
        }
        if (node.left == null && node.right == null) {
            //deep大于當(dāng)前最大Deep時(shí)才會(huì)更新,所以同一深度的存儲(chǔ)的是左節(jié)點(diǎn),因?yàn)橄缺闅v的左節(jié)點(diǎn)
            if (deep > Deep) {
                value = node.val;
                Deep = deep;
            }
        }
        if (node.left != null) {
            findLeftValue(node.left, deep+1);
        }
        if (node.right != null) {
            findLeftValue(node.right, deep+1);
        }
    }
}

112. 路徑總和

112. 路徑總和 - 力扣(LeetCode)
本題判斷是否存在和為指定值的路徑,那么只需要判斷即可,比較容易

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }
        targetSum -= root.val;
        if (root.left == null && root.right == null) {
            return targetSum == 0;
        }
        if (root.left != null) {
            boolean left = hasPathSum(root.left, targetSum);
            if (left) {
                return true;
            }
        }
        if (root.right != null) {
            boolean right = hasPathSum(root.right, targetSum);
            if (right) {
                return right;
            }
        }
        return false;
    }
}

113.路徑總和ii

113. 路徑總和 II - 力扣(LeetCode)
本題和112不同的是需要找出特定的路徑,與257. 二叉樹(shù)的所有路徑類(lèi)似,但是要注意的是res不能直接add path,因?yàn)閜ath后面會(huì)變,所以需要新建一個(gè)加入

class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) {
            return res;
        }
        List<Integer> paths = new ArrayList<>();
        traversal(root,targetSum,paths,res);
        return res;
    }
    private void traversal(TreeNode node, int targetSum, List<Integer> paths, List<List<Integer>> res) {
        paths.add(node.val);
        // 葉子節(jié)點(diǎn)處理
        if (node.left == null && node.right == null) {
            if (targetSum == node.val) {
                // 后續(xù)paths會(huì)改變,所以需要定義一個(gè)新的列表
                res.add(new ArrayList<>(paths));
            }
            return;
        }
        if (node.left != null) {
            traversal(node.left, targetSum-node.val, paths, res);
            paths.remove(paths.size()-1);
        }
        if (node.right != null) {
            traversal(node.right, targetSum-node.val, paths, res);
            paths.remove(paths.size()-1);
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容