Tag: Binary Tree

  • LeetCode: 226 Invert Binary Tree

    Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 问题 翻转一棵二叉树。 解决方案 1 递归方法 Approach 1 / 方法 1 This solution uses a recursive approach. The…

  • LeetCode: 110 Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: A binary tree in which the left and right subtrees of every node differ in height by no more than 1. Example: Input: root = [3,9,20,null,null,15,7] Output: true Input: root =…

  • Algorithms 101: Comprehensive Guide to Binary Tree Traversals

    Binary tree traversals are fundamental techniques in computer science for exploring and manipulating tree data structures. They are crucial for depth-first search (DFS) algorithms, allowing various operations such as searching, sorting, and structuring data in binary trees. 介绍:二叉树遍历是计算机科学中探索和操作树数据结构的基本技术。它们对于深度优先搜索(DFS)算法至关重要,允许在二叉树中进行搜索、排序和数据结构化等各种操作。 Understanding Binary Tree Traversals: 理解二叉树遍历: Definition: Binary tree traversals involve visiting each…

  • Algorithms 101: Ultimate Guide to Solving Binary Tree Coding Problems with Tips & Techniques

    Algorithms 101: Ultimate Guide to Solving Binary Tree Coding Problems with Tips & Techniques

    Binary Tree Problems and Solutions 1. Sum of Elements in a Binary Tree Python Code: class TreeNode: def __init__(self, value=0, left=None, right=None): self.value = value self.left = left self.right = right def sum_of_elements(root): if root is None: return 0 left_sum = sum_of_elements(root.left) right_sum = sum_of_elements(root.right) return root.value + left_sum +…

  • Algorithms 101: Non-Recursive Binary Tree Traversal

    Non-Recursive Binary Tree Traversal by 砖家王二狗 In-order Traversal: In in-order traversal, we visit the left subtree, then the root, and finally the right subtree. Here’s a non-recursive example in Python: def in_order_traversal(root): result = [] stack = [] current = root while current or stack: while current: stack.append(current) current =…

  • LeetCode: 124 Binary Tree Maximum Path Sum

    Problem Statement Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need…