大道至简

人生苦短,我用Python.

位运算实现加法

位运算实现加法

在计算机的底层使用位运算计算加法,对于减法运算使用其补码转成加法运算的形式;假设不考虑进位,计算两个一位数的加法 1 + 1 = 0 向上进了一个1 1 + 0 = 1 0 + 1 = 1 0 + 0 = 0 从上面的计算结果可以看出,可以使用异或运算来代替+ 1 ^ 1 = 0 1 ^ 0 = 1 0 ^ 1 = 1 0 ^ 0 = 0 这样就完成了简单的一位数加法,但是进行两...

LeetCode-Alg-1022-Sum of Root To Leaf Binary Numbers

LeetCode-Alg-1022-Sum of Root To Leaf Binary Numbers

1、题目 Given a binary tree, each node has value 0 or 1. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is 0 -> 1 -> 1 ->...

LeetCode-Alg-938-Range Sum of BST

LeetCode-Alg-938-Range Sum of BST

1、题目 Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive). The binary search tree is guaranteed to have unique values. Exampl...

LeetCode-Alg-897-Increasing Order Search Tree

LeetCode-Alg-897-Increasing Order Search Tree

1、题目 Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child. Example 1: Input: [5,...

LeetCode-Alg-687-Longest Univalue Path

LeetCode-Alg-687-Longest Univalue Path

1、题目 Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. The length of path between two nodes ...

LeetCode-Alg-669-Trim a Binary Search Tree

LeetCode-Alg-669-Trim a Binary Search Tree

1、题目 Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, ...

LeetCode-Alg-653-Two Sum IV Input is a BST

LeetCode-Alg-653-Two Sum IV Input is a BST

1、题目 Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target. Example 1: Input: 5 / \ 3 6 ...

莫里斯算法

莫里斯算法遍历二叉树

1、二叉树的遍历 对于二叉树的遍历分为三种:前序遍历(根左右)、中序遍历(左根右)、后序遍历(左右根)。假设一个树高为h的二叉树,三种遍历算法的时间复杂度都是O(n),空间复杂度为O(h) 因为遍历二叉树的每一个节点,最好的情况就是每一个节点被访问一次,所以时间复杂度无法再优化,但是空间复杂度仍然可以优化。 二叉树的遍历算法可以使用递归和非递归来实现,递归会有隐式的调用堆栈,非递归使用额外...

LeetCode-Alg-530-Minimum Absolute Difference in BST

LeetCode-Alg-530-Minimum Absolute Difference in BST

1、题目 Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 \ 3 / 2 Output: 1 Explanat...

LeetCode-Alg-437-Path Sum III

LeetCode-Alg-437-Path Sum III

1、题目 You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need to start or end at the root or a leaf, b...

< script src = " / js / bootstrap.min.js " >