⬅ Previous Topic
Check if a Binary Tree is BalancedNext Topic ⮕
Boundary Traversal of a Binary Tree⬅ Previous Topic
Check if a Binary Tree is BalancedNext Topic ⮕
Boundary Traversal of a Binary Treeclass TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def diagonalTraversal(root):
if not root:
return []
result = []
queue = [root]
while queue:
node = queue.pop(0)
while node:
result.append(node.val)
if node.left:
queue.append(node.left)
node = node.right
return result
if __name__ == '__main__':
# Example tree:
# 8
# / \
# 3 10
# / \ \
# 1 6 14
# / \ /
# 4 7 13
root = TreeNode(8,
TreeNode(3, TreeNode(1), TreeNode(6, TreeNode(4), TreeNode(7))),
TreeNode(10, None, TreeNode(14, TreeNode(13))))
print(diagonalTraversal(root))
⬅ Previous Topic
Check if a Binary Tree is BalancedNext Topic ⮕
Boundary Traversal of a Binary TreeYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.