摘要:解題思路這個(gè)題目其實(shí)就是基于先序遍歷,用遞歸和非遞歸思想都可以。遞歸求所有左葉子節(jié)點(diǎn)的和,我們可以將其分解為左子樹的左葉子和右子樹的左葉子和遞歸結(jié)束條件找到左葉子節(jié)點(diǎn),就可以返回該節(jié)點(diǎn)的。代碼非遞歸判斷是否為左葉子節(jié)點(diǎn)遞歸
Sum of Left Leaves
Find the sum of all left leaves in a given binary tree.
Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
1.解題思路
這個(gè)題目其實(shí)就是基于先序遍歷,用遞歸和非遞歸思想都可以。
1)非遞歸:
借助棧,在push節(jié)點(diǎn)的時(shí)候判斷是否是左葉子節(jié)點(diǎn),如果是就累計(jì)進(jìn)sum中。
2)遞歸:
求所有左葉子節(jié)點(diǎn)的和,我們可以將其分解為左子樹的左葉子和+右子樹的左葉子和
遞歸結(jié)束條件:找到左葉子節(jié)點(diǎn),就可以返回該節(jié)點(diǎn)的val。
2.代碼
1) 非遞歸
public class Solution { Stacks=new Stack (); int sum=0; public int sumOfLeftLeaves(TreeNode root) { if(root==null) return 0; pushLeft(root); while(!s.empty()){ TreeNode node=s.pop(); if(node.right!=null) pushLeft(node.right); } return sum; } private void pushLeft(TreeNode root){ TreeNode node=root; while(node!=null){ s.push(node); //判斷是否為左葉子節(jié)點(diǎn) if(node.left!=null&&node.left.left==null&&node.left.right==null) sum+=node.left.val; node=node.left; } } }
2)遞歸
public class Solution { public int sumOfLeftLeaves(TreeNode root) { if(root==null) return 0; int leftsum,rightsum; if(root.left!=null&&root.left.left==null&&root.left.right==null) leftsum=root.left.val; else leftsum=sumOfLeftLeaves(root.left); rightsum=sumOfLeftLeaves(root.right); return leftsum+rightsum; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/69802.html
摘要:解題思路利用遞歸,對(duì)于每個(gè)根節(jié)點(diǎn),只要左子樹和右子樹中有一個(gè)滿足,就返回每次訪問一個(gè)節(jié)點(diǎn),就將該節(jié)點(diǎn)的作為新的進(jìn)行下一層的判斷。代碼解題思路本題的不同點(diǎn)是可以不從開始,不到結(jié)束。代碼當(dāng)前節(jié)點(diǎn)開始當(dāng)前節(jié)點(diǎn)左節(jié)點(diǎn)開始當(dāng)前節(jié)點(diǎn)右節(jié)點(diǎn)開始 Path SumGiven a binary tree and a sum, determine if the tree has a root-to-lea...
摘要:解題思路本題要求所有從根結(jié)點(diǎn)到葉子節(jié)點(diǎn)的路徑和,我們用遞歸實(shí)現(xiàn)。結(jié)束條件當(dāng)遇到葉子節(jié)點(diǎn)時(shí),直接結(jié)束,返回計(jì)算好的如果遇到空節(jié)點(diǎn),則返回?cái)?shù)值。 Sum Root to Leaf NumbersGiven a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a numbe...
Problem Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return ...
摘要:但是本題的難點(diǎn)在于,使用遞歸實(shí)現(xiàn),但是前面的第四種情況不能作為遞歸函數(shù)的返回值,所以我們需要定義兩個(gè)值,代表單邊路徑的最大值,用于遞歸用于和回路的較大值。 Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. For this problem, a path is defined as a...
摘要:解題思路用遞歸實(shí)現(xiàn)很簡(jiǎn)單,對(duì)于每個(gè)根節(jié)點(diǎn),最大深度就等于左子樹的最大深度和右子樹的最大深度的較大值。解題思路本題的注意點(diǎn)在于如果某個(gè)根節(jié)點(diǎn)有一邊的子樹為空,那么它的深度就等于另一邊不為空的子樹的深度,其他的邏輯與上一題相同。 Maximum Depth of Binary TreeGiven a binary tree, find its maximum depth. The maxi...
閱讀 3248·2021-11-23 10:02
閱讀 3187·2021-11-16 11:53
閱讀 3155·2021-09-23 11:21
閱讀 3431·2019-08-30 13:02
閱讀 1693·2019-08-29 16:18
閱讀 1623·2019-08-29 12:55
閱讀 1538·2019-08-26 12:24
閱讀 2172·2019-08-26 10:36