自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

做了這么多題目了,會求左葉子之和么?

開發(fā) 前端
平時我們解二叉樹的題目時,已經(jīng)習(xí)慣了通過節(jié)點的左右孩子判斷本節(jié)點的屬性,而本題我們要通過節(jié)點的父節(jié)點判斷本節(jié)點的屬性。

[[416239]]

本文轉(zhuǎn)載自微信公眾號「代碼隨想錄」,作者程序員Carl 。轉(zhuǎn)載本文請聯(lián)系代碼隨想錄公眾號。

左葉子之和

題目地址:https://leetcode-cn.com/problems/sum-of-left-leaves/

計算給定二叉樹的所有左葉子之和。

示例:

思路

首先要注意是判斷左葉子,不是二叉樹左側(cè)節(jié)點,所以不要上來想著層序遍歷。

因為題目中其實沒有說清楚左葉子究竟是什么節(jié)點,那么我來給出左葉子的明確定義:如果左節(jié)點不為空,且左節(jié)點沒有左右孩子,那么這個節(jié)點就是左葉子

大家思考一下如下圖中二叉樹,左葉子之和究竟是多少?

其實是0,因為這棵樹根本沒有左葉子!

那么判斷當前節(jié)點是不是左葉子是無法判斷的,必須要通過節(jié)點的父節(jié)點來判斷其左孩子是不是左葉子。

如果該節(jié)點的左節(jié)點不為空,該節(jié)點的左節(jié)點的左節(jié)點為空,該節(jié)點的左節(jié)點的右節(jié)點為空,則找到了一個左葉子,判斷代碼如下:

  1. if (node->left != NULL && node->left->left == NULL && node->left->right == NULL) { 
  2.     左葉子節(jié)點處理邏輯 

遞歸法

遞歸的遍歷順序為后序遍歷(左右中),是因為要通過遞歸函數(shù)的返回值來累加求取左葉子數(shù)值之和。。

遞歸三部曲:

1.確定遞歸函數(shù)的參數(shù)和返回值

判斷一個樹的左葉子節(jié)點之和,那么一定要傳入樹的根節(jié)點,遞歸函數(shù)的返回值為數(shù)值之和,所以為int

使用題目中給出的函數(shù)就可以了。

2.確定終止條件

依然是

  1. if (root == NULLreturn 0; 

3.確定單層遞歸的邏輯

當遇到左葉子節(jié)點的時候,記錄數(shù)值,然后通過遞歸求取左子樹左葉子之和,和 右子樹左葉子之和,相加便是整個樹的左葉子之和。

代碼如下:

  1. int leftValue = sumOfLeftLeaves(root->left);    // 左 
  2. int rightValue = sumOfLeftLeaves(root->right);  // 右 
  3.                                                 // 中 
  4. int midValue = 0; 
  5. if (root->left && !root->left->left && !root->left->right) { 
  6.     midValue = root->left->val; 
  7. int sum = midValue + leftValue + rightValue; 
  8. return sum

整體遞歸代碼如下:

  1. class Solution { 
  2. public
  3.     int sumOfLeftLeaves(TreeNode* root) { 
  4.         if (root == NULLreturn 0; 
  5.  
  6.         int leftValue = sumOfLeftLeaves(root->left);    // 左 
  7.         int rightValue = sumOfLeftLeaves(root->right);  // 右 
  8.                                                         // 中 
  9.         int midValue = 0; 
  10.         if (root->left && !root->left->left && !root->left->right) { // 中 
  11.             midValue = root->left->val; 
  12.         } 
  13.         int sum = midValue + leftValue + rightValue; 
  14.         return sum
  15.     } 
  16. }; 

以上代碼精簡之后如下:

  1. class Solution { 
  2. public
  3.     int sumOfLeftLeaves(TreeNode* root) { 
  4.         if (root == NULLreturn 0; 
  5.         int midValue = 0; 
  6.         if (root->left != NULL && root->left->left == NULL && root->left->right == NULL) { 
  7.             midValue = root->left->val; 
  8.         } 
  9.         return midValue + sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right); 
  10.     } 
  11. }; 

迭代法

本題迭代法使用前中后序都是可以的,只要把左葉子節(jié)點統(tǒng)計出來,就可以了,那么參考文章 二叉樹:聽說遞歸能做的,棧也能做!和二叉樹:迭代法統(tǒng)一寫法中的寫法,可以寫出一個前序遍歷的迭代法。

判斷條件都是一樣的,代碼如下:

  1. class Solution { 
  2. public
  3.     int sumOfLeftLeaves(TreeNode* root) { 
  4.         stack<TreeNode*> st; 
  5.         if (root == NULLreturn 0; 
  6.         st.push(root); 
  7.         int result = 0; 
  8.         while (!st.empty()) { 
  9.             TreeNode* node = st.top(); 
  10.             st.pop(); 
  11.             if (node->left != NULL && node->left->left == NULL && node->left->right == NULL) { 
  12.                 result += node->left->val; 
  13.             } 
  14.             if (node->right) st.push(node->right); 
  15.             if (node->left) st.push(node->left); 
  16.         } 
  17.         return result; 
  18.     } 
  19. }; 

總結(jié)

這道題目要求左葉子之和,其實是比較繞的,因為不能判斷本節(jié)點是不是左葉子節(jié)點。

此時就要通過節(jié)點的父節(jié)點來判斷其左孩子是不是左葉子了。

平時我們解二叉樹的題目時,已經(jīng)習(xí)慣了通過節(jié)點的左右孩子判斷本節(jié)點的屬性,而本題我們要通過節(jié)點的父節(jié)點判斷本節(jié)點的屬性。

希望通過這道題目,可以擴展大家對二叉樹的解題思路。

其他語言版本

Java

遞歸

  1. class Solution { 
  2.     public int sumOfLeftLeaves(TreeNode root) { 
  3.         if (root == nullreturn 0; 
  4.         int leftValue = sumOfLeftLeaves(root.left);    // 左 
  5.         int rightValue = sumOfLeftLeaves(root.right);  // 右 
  6.                                                         
  7.         int midValue = 0; 
  8.         if (root.left != null && root.left.left == null && root.left.right == null) { // 中 
  9.             midValue = root.left.val; 
  10.         } 
  11.         int sum = midValue + leftValue + rightValue; 
  12.         return sum
  13.     } 

迭代

  1. class Solution { 
  2.     public int sumOfLeftLeaves(TreeNode root) { 
  3.         if (root == nullreturn 0; 
  4.         Stack<TreeNode> stack = new Stack<> (); 
  5.         stack.add(root); 
  6.         int result = 0; 
  7.         while (!stack.isEmpty()) { 
  8.             TreeNode node = stack.pop(); 
  9.             if (node.left != null && node.left.left == null && node.left.right == null) { 
  10.                 result += node.left.val; 
  11.             } 
  12.             if (node.right != null) stack.add(node.right); 
  13.             if (node.left != null) stack.add(node.left); 
  14.         } 
  15.         return result; 
  16.     } 

Python

遞歸

  1. class Solution: 
  2.     def sumOfLeftLeaves(self, root: TreeNode) -> int
  3.         if not root:  
  4.             return 0 
  5.          
  6.         left_left_leaves_sum = self.sumOfLeftLeaves(root.left)  # 左 
  7.         right_left_leaves_sum = self.sumOfLeftLeaves(root.right) # 右 
  8.          
  9.         cur_left_leaf_val = 0 
  10.         if root.left and not root.left.left and not root.left.right:  
  11.             cur_left_leaf_val = root.left.val  # 中 
  12.              
  13.         return cur_left_leaf_val + left_left_leaves_sum + right_left_leaves_sum 

迭代

  1. class Solution: 
  2.     def sumOfLeftLeaves(self, root: TreeNode) -> int
  3.         ""
  4.         Idea: Each time check current node's left node.  
  5.               If current node don't have one, skip it.  
  6.         ""
  7.         stack = [] 
  8.         if root:  
  9.             stack.append(root) 
  10.         res = 0 
  11.          
  12.         while stack:  
  13.             # 每次都把當前節(jié)點的左節(jié)點加進去.  
  14.             cur_node = stack.pop() 
  15.             if cur_node.left and not cur_node.left.left and not cur_node.left.right:  
  16.                 res += cur_node.left.val 
  17.                  
  18.             if cur_node.left:  
  19.                 stack.append(cur_node.left
  20.             if cur_node.right:  
  21.                 stack.append(cur_node.right
  22.                  
  23.         return res 

 

責任編輯:武曉燕 來源: 代碼隨想錄
相關(guān)推薦

2021-03-24 08:44:11

代碼內(nèi)存消耗語言

2021-06-09 10:10:20

代碼內(nèi)存編程語言

2021-06-14 07:23:42

Windows10操作系統(tǒng)微軟

2018-05-09 11:04:35

Java程序員大數(shù)據(jù)

2024-04-02 08:41:10

ArrayListSubList場景

2017-08-11 14:21:33

軟件開發(fā)前端框架

2023-07-17 08:21:52

漏洞版本項目

2020-12-31 05:49:44

FlinkSQL函數(shù)

2020-12-14 07:31:57

JDKJVM監(jiān)控

2018-06-26 15:00:24

Docker安全風(fēng)險

2024-07-12 09:35:38

前端工具檢驗

2023-11-13 08:49:54

2024-02-20 08:09:51

Java 8DateUtilsDate工具類

2018-03-05 10:40:21

安卓APPGoogle

2023-07-07 19:23:08

微軟文字Claude

2022-01-12 20:04:09

網(wǎng)絡(luò)故障斷網(wǎng)事件網(wǎng)絡(luò)安全

2017-12-21 19:38:50

潤乾中間表

2021-01-14 05:08:44

編譯鏈接

2022-07-26 23:43:29

編程語言開發(fā)Java

2021-01-29 08:52:10

App微信移動應(yīng)用
點贊
收藏

51CTO技術(shù)棧公眾號