二叉树的右视图 python_[LeetCode] 199. Binary Tree Right Side View 二叉树的右侧视图

Python: DFS

# Time: O(n)

# Space: O(h)

class TreeNode(object):

def __init__(self, x):

self.val = x

self.left = None

self.right = None

class Solution(object):

# @param root, a tree node

# @return a list of integers

def rightSideView(self, root):

result = []

self.rightSideViewDFS(root, 1, result)

return result

def rightSideViewDFS(self, node, depth, result):

if not node:

return

if depth > len(result):

result.append(node.val)

self.rightSideViewDFS(node.right, depth+1, result)

self.rightSideViewDFS(node.left, depth+1, result)

Python: BFS

# Time: O(n)

# Space: O(n)

class Solution2(object):

# @param root, a tree node

# @return a list of integers

def rightSideView(self, root):

if root is None:

return []

result, current = [], [root]

while current:

next_level = []

for node in current:

if node.left:

next_level.append(node.left)

if node.right:

next_level.append(node.right)

result.append(node.val)

current = next_level

return result

Python: Compute the right view of both right and left left subtree, then combine them. For very unbalanced trees, this can be O(n^2), though.

def rightSideView(self, root):

if not root:

return []

right = self.rightSideView(root.right)

left = self.rightSideView(root.left)

return [root.val] + right + left[len(right):]

Python: DFS-traverse the tree right-to-left, add values to the view whenever we first reach a new record depth. This is O(n).

def rightSideView(self, root):

def collect(node, depth):

if node:

if depth == len(view):

view.append(node.val)

collect(node.right, depth+1)

collect(node.left, depth+1)

view = []

collect(root, 0)

return view

Python: Traverse the tree level by level and add the last value of each level to the view. This is O(n).

def rightSideView(self, root):

view = []

if root:

level = [root]

while level:

view += level[-1].val,

level = [kid for node in level for kid in (node.left, node.right) if kid]

return view

C++: DFS

class Solution {

public:

void recursion(TreeNode *root, int level, vector &res)

{

if(root==NULL) return ;

if(res.size()val);

recursion(root->right, level+1, res);

recursion(root->left, level+1, res);

}

vector rightSideView(TreeNode *root) {

vector res;

recursion(root, 1, res);

return res;

}

};

C++: BFS

class Solution {

public:

vector rightSideView(TreeNode *root) {

vector res;

if (!root) return res;

queue q;

q.push(root);

while (!q.empty()) {

res.push_back(q.back()->val);

int size = q.size();

for (int i = 0; i < size; ++i) {

TreeNode *node = q.front();

q.pop();

if (node->left) q.push(node->left);

if (node->right) q.push(node->right);

}

}

return res;

}

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值