We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
function bubbleSort(arr) { var len = arr.length; for (var i = 0; i < len; i++) { for (var j = i + 1; j < len; j++) { if (arr[j] < arr[i]) { //相邻元素两两对比 var temp = arr[i]; //元素交换 arr[i] = arr[j]; arr[j] = temp; } } } return arr; }
function binarySearch(arr, key) { var low = 0, high = arr.length - 1; while (low <= high) { var mid = Math.floor((low + high) / 2); if (key == arr[mid]) { return mid; } else if (key < arr[mid]) { high = mid - 1; } else { low = mid + 1; } } return -1; }
function fibonacci (n , a = 0 , b = 1) { if( n === 0 ) {return a}; return fibonacci (n - 1, b, a + b); }
function r(str) { return str.split('').reverse().join('') }
public ListNode reverse(ListNode head) { ListNode prev = null; while (head != null) { ListNode temp = head.next; head.next = prev; prev = head; head = temp; } return prev; }
public Boolean hasCycle(ListNode head) { if (head == null || head.next == null) { return false; } ListNode fast, slow; fast = head.next; slow = head; while (fast != slow) { if(fast==null || fast.next==null) return false; fast = fast.next.next; slow = slow.next; } return true; }
//非递归广度优先实现 function bfs (tree) { if (!tree || !tree.length) return; let stack = []; //先将第一层节点放入栈 for (let i = 0; i < tree.length; i++) { stack.push(tree[i]); } let item; while (stack.length) { item = stack.shift(); console.log(item.id); //如果该节点有子节点,继续添加进入栈底 if (item.children && item.children.length) { stack = stack.concat(item.children); } } }
//递归实现 function parseTreeJson (tree) { if (!tree || !tree.length) return; for (let i = 0; i < tree.length; i++) { let children = tree[i].children; console.log(tree[i].id); if (children && children.length > 0) { parseTreeJson(children); } } } //非递归深度优先实现 function dfs (tree) { if (!tree || !tree.length) return; let stack = []; //先将第一层节点放入栈 for (let i = 0; i < tree.length; i++) { stack.push(tree[i]); } let item; while (stack.length) { item = stack.shift(); console.log(item.id); //如果该节点有子节点,继续添加进入栈顶 if (item.children && item.children.length) { stack = item.children.concat(stack); } } }
function preOrder(node) { if (node) { console.log(node.value); preOrder(node.left); preOrder(node.right); } }
public int maxDepth(TreeNode root) { if (root == null) { return 0; } int left = maxDepth(root.left); int right = maxDepth(root.right); return Math.max(left, right) + 1; }
public int minDepth(TreeNode root) { if (root == null) { return 0; } return getMin(root); } public int getMin(TreeNode root){ if (root == null) { return Integer.MAX_VALUE; } if (root.left == null && root.right == null) { return 1; } return Math.min(getMin(root.left), getMin(root.right)) + 1; }
public void invertBinaryTree(TreeNode root) { if (root == null) { return; } TreeNode temp = root.left; root.left = root.right; root.right = temp; invertBinaryTree(root.left); invertBinaryTree(root.right); }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
实现排序算法
二分查找算法
斐波那契数列
反转字符串
翻转链表
带环链表
广度优先遍历
深度优先遍历
先序遍历
二叉树最大深度
二叉树最小深度
翻转二叉树
The text was updated successfully, but these errors were encountered: