LeetCode刷题笔记(树):binary-tree-preorder-traversal



[toc]

题目描述

Given a binary tree, return the preorder traversal of its nodes’ values.

For example:
Given binary tree{1,#,2,3},

1
\
2
/
3

return[1,2,3].

Note: Recursive solution is trivial, could you do it iteratively?

给定一个二叉树,返回其节点值的前序遍历。

注意:递归解决方案是微不足道的,你可以迭代地做?

解题思路

采用先序遍历的思想(根左右)+数字求和的思想(每一层都比上层和*10+当前根节点的值)。

C++版代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:

int preorder(TreeNode *root, int sum){
if(root == NULL)
return 0;
sum = sum * 10 + root->val;
if(root->left == NULL && root->right == NULL)
return sum;
return preorder(root->left, sum) + preorder(root->right, sum);
}
int sumNumbers(TreeNode *root) {
if(root == NULL)
return 0;
int sum = 0;
return preorder(root, sum);
}
};

系列教程持续发布中,欢迎订阅、关注、收藏、评论、点赞哦~~( ̄▽ ̄~)~

完的汪(∪。∪)。。。zzz

0%