mirror of
https://github.com/krahets/LeetCode-Book.git
synced 2026-01-12 00:19:02 +08:00
[Update]
1. Update topic names of Pyhon file. 2. Replace the binary tree printer with a more clear one. 3. Update README.
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
__pycache__
|
||||
.vscode
|
||||
.DS_Store
|
||||
@@ -10,7 +10,7 @@ LeetBook《图解算法数据结构》面向算法初学者、互联网求职者
|
||||
|
||||
### :green_book: 剑指 Offer 图文题解
|
||||
|
||||
- 图文详解 75 道题目,覆盖主要算法知识点,非常适合作为算法学习的**第一份题库**。
|
||||
- 图文详解 75 道题目,覆盖主要算法知识点,非常适合作为算法学习的 **第一份题库** 。
|
||||
- 题库活跃于各大互联网公司招聘中,可使笔面试准备事半功倍。
|
||||
- 致力于行文深入浅出、图文搭配,提供简洁的 **Python3, Java, C++** 解题代码。
|
||||
- 笔者整理了 **30 天刷题计划**、**题目分类**,让刷题有迹可循。
|
||||
@@ -35,8 +35,8 @@ LeetBook《图解算法数据结构》面向算法初学者、互联网求职者
|
||||
|
||||
> 建议对数据结构不熟悉的同学,先看这篇熟悉用法。
|
||||
|
||||
- 常用数据结构的**分类**和**基本特点**;
|
||||
- 在算法解题中,数据结构的**常用操作**;
|
||||
- 常用数据结构的**分类**和**基本特点**。
|
||||
- 在算法解题中,数据结构的**常用操作**。
|
||||
- 在 Python3 , Java , C++ 语言中,各数据结构的**初始化与构建方法**。
|
||||
|
||||
### 「[算法复杂度](https://leetcode-cn.com/leetbook/read/illustration-of-algorithm/r84gmi/)」
|
||||
|
||||
@@ -12,130 +12,186 @@
|
||||
#include "ListNode.hpp"
|
||||
#include "TreeNode.hpp"
|
||||
|
||||
/**
|
||||
* @brief Find an element in a vector
|
||||
*
|
||||
* @tparam T
|
||||
* @param vec
|
||||
* @param ele
|
||||
* @return int
|
||||
*/
|
||||
template <typename T>
|
||||
int vecFind(const vector<T>& vec, T ele) {
|
||||
int j = INT_MAX;
|
||||
for (int i = 0; i < vec.size(); i++) {
|
||||
if (vec[i] == ele) {
|
||||
j = i;
|
||||
class PrintUtil {
|
||||
public:
|
||||
/**
|
||||
* @brief Find an element in a vector
|
||||
*
|
||||
* @tparam T
|
||||
* @param vec
|
||||
* @param ele
|
||||
* @return int
|
||||
*/
|
||||
template <typename T>
|
||||
static int vecFind(const vector<T>& vec, T ele) {
|
||||
int j = INT_MAX;
|
||||
for (int i = 0; i < vec.size(); i++) {
|
||||
if (vec[i] == ele) {
|
||||
j = i;
|
||||
}
|
||||
}
|
||||
return j;
|
||||
}
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Concatenate a vector with a delim
|
||||
*
|
||||
* @tparam T
|
||||
* @param delim
|
||||
* @param vec
|
||||
* @return string
|
||||
*/
|
||||
template <typename T>
|
||||
string join(const string& delim, const T& vec) {
|
||||
ostringstream s;
|
||||
for (const auto& i : vec) {
|
||||
if (&i != &vec[0]) {
|
||||
s << delim;
|
||||
/**
|
||||
* @brief Concatenate a vector with a delim
|
||||
*
|
||||
* @tparam T
|
||||
* @param delim
|
||||
* @param vec
|
||||
* @return string
|
||||
*/
|
||||
template <typename T>
|
||||
static string strJoin(const string& delim, const T& vec) {
|
||||
ostringstream s;
|
||||
for (const auto& i : vec) {
|
||||
if (&i != &vec[0]) {
|
||||
s << delim;
|
||||
}
|
||||
s << i;
|
||||
}
|
||||
return s.str();
|
||||
}
|
||||
s << i;
|
||||
}
|
||||
return s.str();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Repeat a string for n times
|
||||
*
|
||||
* @param str
|
||||
* @param n
|
||||
* @return string
|
||||
*/
|
||||
string repeat(string str, int n) {
|
||||
ostringstream os;
|
||||
for(int i = 0; i < n; i++)
|
||||
os << str;
|
||||
return os.str();
|
||||
}
|
||||
/**
|
||||
* @brief Repeat a string for n times
|
||||
*
|
||||
* @param str
|
||||
* @param n
|
||||
* @return string
|
||||
*/
|
||||
static string strRepeat(string str, int n) {
|
||||
ostringstream os;
|
||||
for(int i = 0; i < n; i++)
|
||||
os << str;
|
||||
return os.str();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the Vector String object
|
||||
*
|
||||
* @tparam T
|
||||
* @param list
|
||||
* @return string
|
||||
*/
|
||||
template <typename T>
|
||||
string getVectorString(vector<T> &list) {
|
||||
return "[" + join(", ", list) + "]";
|
||||
}
|
||||
/**
|
||||
* @brief Get the Vector String object
|
||||
*
|
||||
* @tparam T
|
||||
* @param list
|
||||
* @return string
|
||||
*/
|
||||
template <typename T>
|
||||
static string getVectorString(vector<T> &list) {
|
||||
return "[" + strJoin(", ", list) + "]";
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a vector
|
||||
*
|
||||
* @tparam T
|
||||
* @param list
|
||||
*/
|
||||
template <typename T>
|
||||
void printVector(vector<T> &list) {
|
||||
cout << getVectorString(list) << '\n';
|
||||
}
|
||||
/**
|
||||
* @brief Print a vector
|
||||
*
|
||||
* @tparam T
|
||||
* @param list
|
||||
*/
|
||||
template <typename T>
|
||||
static void printVector(vector<T> &list) {
|
||||
cout << getVectorString(list) << '\n';
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a vector matrix
|
||||
*
|
||||
* @tparam T
|
||||
* @param matrix
|
||||
*/
|
||||
template <typename T>
|
||||
void printVectorMatrix(vector<vector<T>> &matrix) {
|
||||
cout << "[" << '\n';
|
||||
for (vector<T> &list : matrix)
|
||||
cout << " " + getVectorString(list) + "," << '\n';
|
||||
cout << "]" << '\n';
|
||||
}
|
||||
/**
|
||||
* @brief Print a vector matrix
|
||||
*
|
||||
* @tparam T
|
||||
* @param matrix
|
||||
*/
|
||||
template <typename T>
|
||||
static void printVectorMatrix(vector<vector<T>> &matrix) {
|
||||
cout << "[" << '\n';
|
||||
for (vector<T> &list : matrix)
|
||||
cout << " " + getVectorString(list) + "," << '\n';
|
||||
cout << "]" << '\n';
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a linked list
|
||||
*
|
||||
* @param head
|
||||
*/
|
||||
void printLinkedList(ListNode *head) {
|
||||
vector<int> list;
|
||||
while (head != nullptr) {
|
||||
list.push_back(head->val);
|
||||
head = head->next;
|
||||
}
|
||||
|
||||
cout << join(" -> ", list) << '\n';
|
||||
}
|
||||
/**
|
||||
* @brief Print a linked list
|
||||
*
|
||||
* @param head
|
||||
*/
|
||||
static void printLinkedList(ListNode *head) {
|
||||
vector<int> list;
|
||||
while (head != nullptr) {
|
||||
list.push_back(head->val);
|
||||
head = head->next;
|
||||
}
|
||||
|
||||
cout << strJoin(" -> ", list) << '\n';
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print helper for binary tree
|
||||
*
|
||||
* @param root
|
||||
* @param level
|
||||
*/
|
||||
void printTreeHelper(TreeNode *root, int level) {
|
||||
if (root == nullptr)
|
||||
return;
|
||||
printTreeHelper(root->right, level + 1);
|
||||
cout << repeat(" ", 4 * level) + "->" << root->val << '\n';
|
||||
printTreeHelper(root->left, level + 1);
|
||||
}
|
||||
/**
|
||||
* @brief This tree printer is borrowed from TECHIE DELIGHT
|
||||
* https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
*/
|
||||
struct Trunk {
|
||||
Trunk *prev;
|
||||
string str;
|
||||
Trunk(Trunk *prev, string str) {
|
||||
this->prev = prev;
|
||||
this->str = str;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Helper function to print branches of the binary tree
|
||||
*
|
||||
* @param p
|
||||
*/
|
||||
static void showTrunks(Trunk *p) {
|
||||
if (p == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
showTrunks(p->prev);
|
||||
cout << p->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a binary tree (90º counter-clockwise rotated)
|
||||
*
|
||||
* @param root
|
||||
*/
|
||||
void printTree(TreeNode *root) {
|
||||
printTreeHelper(root, 0);
|
||||
}
|
||||
/**
|
||||
* @brief The interface of the tree printer
|
||||
*
|
||||
* @param root
|
||||
*/
|
||||
static void printTree(TreeNode *root) {
|
||||
printTree(root, nullptr, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a binary tree
|
||||
*
|
||||
* @param root
|
||||
* @param prev
|
||||
* @param isLeft
|
||||
*/
|
||||
static void printTree(TreeNode *root, Trunk *prev, bool isLeft) {
|
||||
if (root == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
string prev_str = " ";
|
||||
Trunk *trunk = new Trunk(prev, prev_str);
|
||||
|
||||
printTree(root->right, trunk, true);
|
||||
|
||||
if (!prev) {
|
||||
trunk->str = "———";
|
||||
}
|
||||
else if (isLeft) {
|
||||
trunk->str = "/———";
|
||||
prev_str = " |";
|
||||
}
|
||||
else {
|
||||
trunk->str = "\\———";
|
||||
prev->str = prev_str;
|
||||
}
|
||||
|
||||
showTrunks(trunk);
|
||||
cout << " " << root->val << endl;
|
||||
|
||||
if (prev) {
|
||||
prev->str = prev_str;
|
||||
}
|
||||
trunk->str = " |";
|
||||
|
||||
printTree(root->left, trunk, false);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -28,7 +28,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
vector<int> res = slt->reversePrint(head);
|
||||
printVector(res);
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
vector<int> res = slt->reversePrint(head);
|
||||
printVector(res);
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
TreeNode* res = slt->buildTree(preorder, inorder);
|
||||
printTree(res);
|
||||
PrintUtil::printTree(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ int main() {
|
||||
cQueue->appendTail(2);
|
||||
res.push_back(cQueue->deleteHead());
|
||||
res.push_back(cQueue->deleteHead());
|
||||
printVector(res);
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -28,7 +28,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
ListNode* res = slt->deleteNode(head, val);
|
||||
printLinkedList(res);
|
||||
PrintUtil::printLinkedList(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
vector<int> res = slt->exchange(nums);
|
||||
printVector(res);
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
ListNode* res = slt->getKthFromEnd(head, k);
|
||||
printLinkedList(res);
|
||||
PrintUtil::printLinkedList(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
ListNode* res = slt->getKthFromEnd(head, k);
|
||||
printLinkedList(res);
|
||||
PrintUtil::printLinkedList(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
ListNode* res = slt->reverseList(head);
|
||||
printLinkedList(res);
|
||||
PrintUtil::printLinkedList(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
ListNode* res = slt->reverseList(head);
|
||||
printLinkedList(res);
|
||||
PrintUtil::printLinkedList(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
ListNode* res = slt->mergeTwoLists(l1, l2);
|
||||
printLinkedList(res);
|
||||
PrintUtil::printLinkedList(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
TreeNode* res = slt->mirrorTree(root);
|
||||
printTree(res);
|
||||
PrintUtil::printTree(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
TreeNode* res = slt->mirrorTree(root);
|
||||
printTree(res);
|
||||
PrintUtil::printTree(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
vector<int> res = slt->spiralOrder(matrix);
|
||||
printVector(res);
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ int main() {
|
||||
minStack->pop();
|
||||
res.push_back(minStack->top());
|
||||
res.push_back(minStack->min());
|
||||
printVector(res);
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -31,7 +31,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
vector<int> res = slt->levelOrder(root);
|
||||
printVector(res);
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
vector<vector<int>> res = slt->levelOrder(root);
|
||||
printVectorMatrix(res);
|
||||
PrintUtil::printVectorMatrix(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
vector<vector<int>> res = slt->levelOrder(root);
|
||||
printVectorMatrix(res);
|
||||
PrintUtil::printVectorMatrix(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
vector<vector<int>> res = slt->levelOrder(root);
|
||||
printVectorMatrix(res);
|
||||
PrintUtil::printVectorMatrix(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
vector<vector<int>> res = slt->pathSum(root, sum);
|
||||
printVectorMatrix(res);
|
||||
PrintUtil::printVectorMatrix(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -74,9 +74,9 @@ int main() {
|
||||
for (int i = 0; i < nodeListNew.size(); i++) {
|
||||
Node* node = nodeListNew[i];
|
||||
printArr[i][0] = node->val;
|
||||
printArr[i][1] = vecFind(nodeListNew, node->random);
|
||||
printArr[i][1] = PrintUtil::vecFind(nodeListNew, node->random);
|
||||
}
|
||||
printVectorMatrix(printArr);
|
||||
PrintUtil::printVectorMatrix(printArr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -84,9 +84,9 @@ int main() {
|
||||
for (int i = 0; i < nodeListNew.size(); i++) {
|
||||
Node* node = nodeListNew[i];
|
||||
printArr[i][0] = node->val;
|
||||
printArr[i][1] = vecFind(nodeListNew, node->random);
|
||||
printArr[i][1] = PrintUtil::vecFind(nodeListNew, node->random);
|
||||
}
|
||||
printVectorMatrix(printArr);
|
||||
PrintUtil::printVectorMatrix(printArr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ int main() {
|
||||
nodesVal.push_back(res->val);
|
||||
res = res->right;
|
||||
}
|
||||
cout << join(" <-> ", nodesVal) << endl;
|
||||
cout << PrintUtil::strJoin(" <-> ", nodesVal) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -75,6 +75,6 @@ int main() {
|
||||
Codec* codec = new Codec();
|
||||
TreeNode* root = codec->deserialize(data);
|
||||
string res = codec->serialize(root);
|
||||
printTree(root);
|
||||
PrintUtil::printTree(root);
|
||||
cout << res << endl;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
vector<string> res = slt->permutation(s);
|
||||
printVector(res);
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
vector<int> res = slt->getLeastNumbers(arr, k);
|
||||
printVector(res);
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
vector<int> res = slt->getLeastNumbers(arr, k);
|
||||
printVector(res);
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ int main() {
|
||||
res.push_back(medianFinder->findMedian());
|
||||
medianFinder->addNum(3);
|
||||
res.push_back(medianFinder->findMedian());
|
||||
printVector(res);
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -29,7 +29,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
vector<int> res = slt->singleNumbers(nums);
|
||||
printVector(res);
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
vector<int> res = slt->twoSum(nums, target);
|
||||
printVector(res);
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
vector<vector<int>> res = slt->findContinuousSequence(target);
|
||||
printVectorMatrix(res);
|
||||
PrintUtil::printVectorMatrix(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
vector<vector<int>> res = slt->findContinuousSequence(target);
|
||||
printVectorMatrix(res);
|
||||
PrintUtil::printVectorMatrix(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ int main() {
|
||||
res.push_back(maxQueue->max_value());
|
||||
res.push_back(maxQueue->pop_front());
|
||||
res.push_back(maxQueue->max_value());
|
||||
printVector(res);
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -30,7 +30,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
vector<double> res = slt->dicesProbability(n);
|
||||
printVector(res);
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ int main() {
|
||||
// ====== Driver Code ======
|
||||
Solution* slt = new Solution();
|
||||
vector<int> res = slt->constructArr(a);
|
||||
printVector(res);
|
||||
PrintUtil::printVector(res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,16 @@ package include;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
class Trunk {
|
||||
Trunk prev;
|
||||
String str;
|
||||
|
||||
Trunk(Trunk prev, String str) {
|
||||
this.prev = prev;
|
||||
this.str = str;
|
||||
}
|
||||
};
|
||||
|
||||
public class PrintUtil {
|
||||
/**
|
||||
* Print a linked list
|
||||
@@ -17,23 +27,58 @@ public class PrintUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a binary tree (90º counter-clockwise rotated)
|
||||
* The interface of the tree printer
|
||||
* This tree printer is borrowed from TECHIE DELIGHT
|
||||
* https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
* @param root
|
||||
*/
|
||||
public static void printTree(TreeNode root) {
|
||||
printTreeHelper(root, 0);
|
||||
printTree(root, null, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print helper for binary tree
|
||||
* Print a binary tree
|
||||
* @param root
|
||||
* @param level
|
||||
* @param prev
|
||||
* @param isLeft
|
||||
*/
|
||||
private static void printTreeHelper(TreeNode root, int level) {
|
||||
if (root == null)
|
||||
public static void printTree(TreeNode root, Trunk prev, boolean isLeft) {
|
||||
if (root == null) {
|
||||
return;
|
||||
printTreeHelper(root.right, level + 1);
|
||||
System.out.println(" ".repeat(4 * level) + "->" + root.val);
|
||||
printTreeHelper(root.left, level + 1);
|
||||
}
|
||||
|
||||
String prev_str = " ";
|
||||
Trunk trunk = new Trunk(prev, prev_str);
|
||||
|
||||
printTree(root.right, trunk, true);
|
||||
|
||||
if (prev == null) {
|
||||
trunk.str = "———";
|
||||
} else if (isLeft) {
|
||||
trunk.str = "/———";
|
||||
prev_str = " |";
|
||||
} else {
|
||||
trunk.str = "\\———";
|
||||
prev.str = prev_str;
|
||||
}
|
||||
|
||||
showTrunks(trunk);
|
||||
System.out.println(" " + root.val);
|
||||
|
||||
if (prev != null) {
|
||||
prev.str = prev_str;
|
||||
}
|
||||
trunk.str = " |";
|
||||
|
||||
printTree(root.left, trunk, false);
|
||||
}
|
||||
|
||||
public static void showTrunks(Trunk p) {
|
||||
if (p == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
showTrunks(p.prev);
|
||||
System.out.print(p.str);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,16 +28,46 @@ def print_linked_list(head):
|
||||
arr = linked_list_to_list(head)
|
||||
print(' -> '.join([str(a) for a in arr]))
|
||||
|
||||
def print_tree(root):
|
||||
"""Print a binary tree (90º counter-clockwise rotated)
|
||||
|
||||
class Trunk:
|
||||
def __init__(self, prev=None, str=None):
|
||||
self.prev = prev
|
||||
self.str = str
|
||||
|
||||
def showTrunks(p):
|
||||
if p is None:
|
||||
return
|
||||
showTrunks(p.prev)
|
||||
print(p.str, end='')
|
||||
|
||||
def print_tree(root, prev=None, isLeft=False):
|
||||
"""Print a binary tree
|
||||
This tree printer is borrowed from TECHIE DELIGHT
|
||||
https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
Args:
|
||||
root ([type]): [description]
|
||||
"""
|
||||
def helper(root, level):
|
||||
if not root:
|
||||
return
|
||||
helper(root.right, level + 1)
|
||||
print(' ' * 4 * level + '->', root.val)
|
||||
helper(root.left, level + 1)
|
||||
helper(root, 0)
|
||||
prev ([type], optional): [description]. Defaults to None.
|
||||
isLeft (bool, optional): [description]. Defaults to False.
|
||||
"""
|
||||
if root is None:
|
||||
return
|
||||
|
||||
prev_str = ' '
|
||||
trunk = Trunk(prev, prev_str)
|
||||
print_tree(root.right, trunk, True)
|
||||
|
||||
if prev is None:
|
||||
trunk.str = '———'
|
||||
elif isLeft:
|
||||
trunk.str = '/———'
|
||||
prev_str = ' |'
|
||||
else:
|
||||
trunk.str = '\———'
|
||||
prev.str = prev_str
|
||||
|
||||
showTrunks(trunk)
|
||||
print(' ' + str(root.val))
|
||||
if prev:
|
||||
prev.str = prev_str
|
||||
trunk.str = ' |'
|
||||
print_tree(root.left, trunk, False)
|
||||
|
||||
Reference in New Issue
Block a user